bra*_*e07 1 python if-statement python-3.x
我正在制作一个程序,可以将您的体重从公斤转换为磅,反之亦然。它会提示你输入一个重量,询问它是公斤还是磅,然后给你结果。
代码如下:
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)
Run Code Online (Sandbox Code Playgroud)
如果我输入公斤并说它是公斤,转换器工作正常,但是当我将我的体重输入磅并说它以磅为单位时,它假定该值以公斤为单位并以磅为单位给出答案。谁能告诉我这是什么问题?
如果没有 '()',您就不会调用上层方法,而只是引用它。
unit.upper 将返回('0x00630240 处 str 对象的内置方法上层'),
因此,当您设置 unit = unit.upper 时,
您实际上是在 0x00630240 处设置 unit = 'str object 的内置方法 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
143 次 |
| 最近记录: |