我正在制作一个程序,可以将您的体重从公斤转换为磅,反之亦然。它会提示您输入重量,询问它是公斤还是磅,然后给出结果。
代码如下:
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 将返回("str 对象的内置方法上部"0x00630240"(,
因此,当您设置单位 = 单位.upper时,
您实际上是在设置 unit ='str 对象的内置方法上部在0x00630240',
激活了 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)