HighTemp1= input()
print(‘Temperature 1:’ + HighTemp1)
LowTemp2= input()
print(‘Temperature 2:’ + LowTemp2)
if (HighTemp1>80):
print( ‘The high temperature was Hot’)
elif(HighTemp1> 40 and <80):
print(‘The high temperature was Average’)
else:
print(‘The high temperature was Cold’)
if (LowTemp2>80):
print( ‘The low temperature was Hot’)
elif(LowTemp2> 40 and <80):
print(‘The low temperature was Average’)
else:
print(‘The low temperature was Cold’)因此,我假设确定一天内两个用户输入温度的类别,并以以下方式输出:
Temp Category
>80 Hot
40 to 80 Average
<40 ColdExample#1:温度1: 81温度2: 37高温为热低温为冷
我在这段代码中遇到了太多的错误,我不确定哪里出了问题。代码不会在IDLE中运行,因为当我逐行运行它时(因为我得到了一个多语句错误),要么是类型错误"cannot use > operator with The int and string“,要么是语法错误。我有什么不理解的地方使我的代码无法运行?
发布于 2020-02-16 11:40:00
我假设您使用的是Python3。您的代码中有许多错误
HighTemp1= int(input()) #to take int input, you need to parse it to int. as input() directly reads a string
print("Temperature 1:" + str(HighTemp1)) #print in python3 assumes string in ""
LowTemp2= int(input())
print("Temperature 2:" + str(LowTemp2)) #int can't be directly concatenated to string. You need to parse it to string using str() to concat it with string
if (HighTemp1>80):
print( "The high temperature was Hot")
elif(HighTemp1> 40 and <80):
print("The high temperature was Average")
else:
print("The high temperature was Cold")
if (LowTemp2>80):
print( "The low temperature was Hot")
elif(LowTemp2> 40 and <80):
print("The low temperature was Average")
else:
print("The low temperature was Cold")发布于 2020-02-16 12:06:41
首先,input()返回一个字符串。为了进行比较,您需要将其转换为int类型。第二,注意你的“语录”。在字符串中用作引号的字符不是简单的ASCII。我把它们都换了。第三,你的比较是有缺陷的。我重做了“高”的部分,只修复了“低”的部分。你可以看到不同之处。此外,input()方法将字符串作为输入参数输出给用户,这样他/她就知道要输入什么了。这可能是一件好事。
HighTemp1 = int(input('High: ').strip())
print('Temperature 1: %d' % HighTemp1)
LowTemp2= int(input('Low: ').strip())
print('Temperature 2: %d' % LowTemp2)
if HighTemp1 > 80:
print('The high temperature was Hot')
# elif 40 < HighTemp1 <= 80: # simplifies to next line
elif HighTemp1 > 40:
print('The high temperature was Average')
else:
print('The high temperature was Cold')
if (LowTemp2 > 80): # parens not required here
print('The low temperature was Hot')
elif(LowTemp2 > 40): # and LowTemp2<=80):
# # the above is redundant
# note that your original "and LowTemp2<80" introduced a bug for value 80
print('The low temperature was Average')
else:
print('The low temperature was Cold')https://stackoverflow.com/questions/60245135
复制相似问题