Python 关于我所做的一个小转换器的IF语句问题
我正在制作一个程序,将你的体重从千克转换为磅,反之亦然。它会提示您输入一个重量,询问它的单位是千克还是磅,然后给出结果 代码如下:Python 关于我所做的一个小转换器的IF语句问题,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我正在制作一个程序,将你的体重从千克转换为磅,反之亦然。它会提示您输入一个重量,询问它的单位是千克还是磅,然后给出结果 代码如下: weight = int(input("What is your weight? ")) unit = input ("(L)bs or (K)g? ") unit = unit.upper if unit == "L": converted = (weight * 0.45) print(converted) else: converte
weight = int(input("What is your weight? "))
unit = input ("(L)bs or (K)g? ")
unit = unit.upper
if unit == "L":
converted = (weight * 0.45)
print(converted)
else:
converted = (weight // 0.45)
print(converted)
如果我输入kg并说它是kg,转换器工作正常,但当我输入lbs并说它是lbs时,它假设值是kg,并以lbs为单位给出答案。有人能告诉我问题出在哪里吗?你应该添加到unit.upper的末尾。
如果没有,则不调用upper方法,而只引用它。unit.upper将返回“0x00630240处str对象的内置方法upper”,因此,当设置unit=unit.upper时,实际上是设置unit=“0x00630240处str对象的内置方法upper”,它激活了else语句
weight = int(input("What is your weight? "))
unit = input ("(L)bs or (K)g? ")
unit = unit.upper()
if unit == "L":
converted = (weight * 0.45)
print(converted)
else:
converted = (weight // 0.45)
print(converted)
您应该添加到unit.upper的末尾。
如果没有,则不调用upper方法,而只引用它。unit.upper将返回“0x00630240处str对象的内置方法upper”,因此,当设置unit=unit.upper时,实际上是设置unit=“0x00630240处str对象的内置方法upper”,它激活了else语句
weight = int(input("What is your weight? "))
unit = input ("(L)bs or (K)g? ")
unit = unit.upper()
if unit == "L":
converted = (weight * 0.45)
print(converted)
else:
converted = (weight // 0.45)
print(converted)
尝试将unit.upper更改为unit.upper尝试将unit.upper更改为unit.upper