Python 我的程序在3-10摄氏度之间给出了错误的答案
我三天前开始编程。这显示了3-10 C之间的错误结果,我如何修复它Python 我的程序在3-10摄氏度之间给出了错误的答案,python,Python,我三天前开始编程。这显示了3-10 C之间的错误结果,我如何修复它 x = input("what C is it today: ") if x < str(20): print("Its cold") print("Wear a jacket") if str(20) <= x < str(30): print("Its a nice day") print(&qu
x = input("what C is it today: ")
if x < str(20):
print("Its cold")
print("Wear a jacket")
if str(20) <= x < str(30):
print("Its a nice day")
print("don't forget to have fun")
if x >= str(30):
print("Its hot")
print("open the air conditioner")
x=input(“今天的C是什么:”)
如果x
不要使用str(20),…
使用int(x)
这是因为比较字符串和字符串有点不同
尝试以下方法:
x = input("what C is it today: ")
x = int(x) # here we cast x to an Integer
if x < 20:
print("Its cold", "Wear a jacket") # you can pass multiple value
if 20 <= x < 30:
print("Its a nice day", "don't forget to have fun")
if x >= 30:
print("Its hot", "open the air conditioner")
x=input(“今天的C是什么:”)
x=int(x)#这里我们将x转换为整数
如果x<20:
打印(“天气冷”,“穿夹克”)#您可以传递多个值
如果20=30:
打印(“它很热”,“打开空调”)
不要使用str(20),…
使用int(x)
这是因为比较字符串和字符串有点不同
尝试以下方法:
x = input("what C is it today: ")
x = int(x) # here we cast x to an Integer
if x < 20:
print("Its cold", "Wear a jacket") # you can pass multiple value
if 20 <= x < 30:
print("Its a nice day", "don't forget to have fun")
if x >= 30:
print("Its hot", "open the air conditioner")
x=input(“今天的C是什么:”)
x=int(x)#这里我们将x转换为整数
如果x<20:
打印(“天气冷”,“穿夹克”)#您可以传递多个值
如果20=30:
打印(“它很热”,“打开空调”)
正如Mehrdad所说,input()假定您键入的是字符串。因此,您应该将输入转换为int类型
我可以发现您可以对代码进行的其他改进。例如:
使用elif
和else
语句处理相互关联/在消除过程中出现的情况(例如,如果不是1,则是2)
x=int(输入(“今天是什么C?”)#输入(字符串到整数)
如果x<20:
打印(“天气冷”,“穿夹克”)#您可以传递多个值
elif 20正如Mehrdad所说,input()假定您键入的是字符串。因此,您应该将输入转换为int类型
我可以发现您可以对代码进行的其他改进。例如:
使用elif
和else
语句处理相互关联/在消除过程中出现的情况(例如,如果不是1,则是2)
x=int(输入(“今天是什么C?”)#输入(字符串到整数)
如果x<20:
打印(“天气冷”,“穿夹克”)#您可以传递多个值
请重新粘贴代码并正确格式化。它不是很清晰。还要注意您预期会发生什么,以及实际发生的情况。如果您想使用整数比较,您应该尝试使用int(x)<20
。您使用```
来执行块代码x=int(输入(“现在是什么样的C”)
将避免多次转换。您的问题是“5”>“30”
当你期望5<30
时,请重新粘贴你的代码并正确格式化。它不是很清晰。还要注意你期望发生的事情和实际发生的事情。如果你想使用整数比较,你应该尝试int(x)<20
。您使用`
来执行块代码x=int(输入(“今天是什么C?”)
将避免多次转换。您的问题是“5”>“30”
而您期望5<30