Python “我的程序同时运行两个”;“如果声明”;即使他们不正确。我到底做错了什么?
我正在做一个程序,将单位从英制转换为公制,反之亦然。到目前为止,我只得到了温度转换。当我运行代码时,它可以正常工作,直到它到达“if temputit==\uuuuuuuuuuuuuuu”的if语句。无论我输入什么,即使它是乱七八糟的,它仍然会运行华氏语句(最上面的一个)。下面是我的代码:Python “我的程序同时运行两个”;“如果声明”;即使他们不正确。我到底做错了什么?,python,Python,我正在做一个程序,将单位从英制转换为公制,反之亦然。到目前为止,我只得到了温度转换。当我运行代码时,它可以正常工作,直到它到达“if temputit==\uuuuuuuuuuuuuuu”的if语句。无论我输入什么,即使它是乱七八糟的,它仍然会运行华氏语句(最上面的一个)。下面是我的代码: unit = input(print("What would you like to convert (Temperature, Distance, Length, Wheight, Volume)
unit = input(print("What would you like to convert (Temperature, Distance, Length, Wheight, Volume)? "))
if unit == 'Temperature':
tempunit = input(print('Do you want to convert Celsius or Fahrenheit?'))
if tempunit == "Fahrenheit" or "fahrenheit":
ftemp = int(input(print("Type the number you want to convert: ")))
ftemp_new = (ftemp - 32) * 5/9
print(ftemp_new, "°C")
elif tempunit == "Celsius" or "celsius":
print("ok")
如果tempunit==“Fahrenheit”或“Fahrenheit”为真,则此代码行转换为。第二部分始终为真,因此始终执行if
块。有关详细信息,请参阅
要解决此问题,可以执行以下操作之一:
tempunit==“华氏”或tempunit==“华氏”
tempunit in[“华氏”、“华氏”]
tempunit.lower()=“华氏度”
如果tempunit==“Fahrenheit”或“Fahrenheit”为真,则此代码行转换为。第二部分始终为真,因此始终执行if
块。有关详细信息,请参阅
要解决此问题,可以执行以下操作之一:
tempunit==“华氏”或tempunit==“华氏”
tempunit in[“华氏”、“华氏”]
tempunit.lower()=“华氏度”
当您执行第行时,如果tempunit==“Fahrenheit”或“Fahrenheit”:
,则第二部分的计算结果不会是tempunit是否等于“Fahrenheit”,而是会计算为fahreneit字符串。字符串将始终为true,因此代码将始终执行
请尝试此代码来修复它
unit = input(print("What would you like to convert (Temperature, Distance, Length, Wheight, Volume)? "))
if unit == 'Temperature':
tempunit = input(print('Do you want to convert Celsius or Fahrenheit?'))
if tempunit == "Fahrenheit" or tempunit == "fahrenheit":
ftemp = int(input(print("Type the number you want to convert: ")))
ftemp_new = (ftemp - 32) * 5/9
print(ftemp_new, "°C")
elif tempunit == "Celsius" or tempunit == "celsius":
print("ok")
当您执行第行时,如果tempunit==“Fahrenheit”或“Fahrenheit”:
,则第二部分的计算结果不会是tempunit是否等于“Fahrenheit”,而是会计算为fahreneit字符串。字符串将始终为true,因此代码将始终执行
请尝试此代码来修复它
unit = input(print("What would you like to convert (Temperature, Distance, Length, Wheight, Volume)? "))
if unit == 'Temperature':
tempunit = input(print('Do you want to convert Celsius or Fahrenheit?'))
if tempunit == "Fahrenheit" or tempunit == "fahrenheit":
ftemp = int(input(print("Type the number you want to convert: ")))
ftemp_new = (ftemp - 32) * 5/9
print(ftemp_new, "°C")
elif tempunit == "Celsius" or tempunit == "celsius":
print("ok")
与
if bool("fahrenheit"):
if "fahrenheit" != "":
与
if bool("fahrenheit"):
if "fahrenheit" != "":
因此,将字符串强制转换为布尔值实际上与使用空字符串比较不等式相同
if tempunit == "Fahrenheit" or tempunit == "fahrenheit":
是正确的代码。如果尝试构造列表并使用in运算符,或者通过调用.lower()构造新字符串,则从对象构造到内存分配的速度会稍慢一些
与
if bool("fahrenheit"):
if "fahrenheit" != "":
与
if bool("fahrenheit"):
if "fahrenheit" != "":
因此,将字符串强制转换为布尔值实际上与使用空字符串比较不等式相同
if tempunit == "Fahrenheit" or tempunit == "fahrenheit":
是正确的代码。尝试构造列表并使用in运算符,或通过调用.lower()构造新字符串,从对象构造到内存分配的速度会稍慢。tempunit==“Fahrenheit”或tempunit==“Fahrenheit”“
将其更改为这样,同样适用于摄氏度检查。tempunit==“华氏度”或tempunit==“华氏度”
将其更改为这样,同样适用于摄氏度检查。您的答案中有什么是我的答案?对不起。当你写你的答案时,我正在写我的答案,然后我离开去叮当作响,当我完成我的答案时,我看到你已经把它贴出来了。你知道两个人回答是可能的。这并不意味着你会否决我的答案。我对你的答案投了赞成票,因为它同样有效。非常感谢你,这真的帮助了我。事实上,字符串并不总是正确的。“”的计算结果为False。请尝试bool(“”)或if“”:print(“True”)进行验证。不管怎样,这是正确的答案。你的答案是什么,我的答案不是?对不起。当你写你的答案时,我正在写我的答案,然后我离开去叮当作响,当我完成我的答案时,我看到你已经把它贴出来了。你知道两个人回答是可能的。这并不意味着你会否决我的答案。我对你的答案投了赞成票,因为它同样有效。非常感谢你,这真的帮助了我。事实上,字符串并不总是正确的。“”的计算结果为False。请尝试bool(“”)或if“”:print(“True”)进行验证。无论如何,这是正确的答案。