Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于我所做的一个小转换器的IF语句问题_Python_Python 3.x_If Statement - Fatal编程技术网

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