Python 为什么在使用lower()和不使用lower()时会得到不同的结果?
当我不使用.lower()时:我没有得到想要的结果Python 为什么在使用lower()和不使用lower()时会得到不同的结果?,python,if-statement,Python,If Statement,当我不使用.lower()时:我没有得到想要的结果 name=input("Insert your name here: \n") weight=float(input("Enter your weight here: \n")) unit=input("What unit do you want to use? \n") if unit == "kgs" or "KGS": print(f"{name} is {weight} kgs.") elif unit == "LBS" or
name=input("Insert your name here: \n")
weight=float(input("Enter your weight here: \n"))
unit=input("What unit do you want to use? \n")
if unit == "kgs" or "KGS":
print(f"{name} is {weight} kgs.")
elif unit == "LBS" or "lbs":
convert=weight/2.2
print(f"{name} is {convert} lbs.")
else:
print("Please re-enter.")
name=input("Insert your name here: \n")
weight=float(input("Enter your weight here: \n"))
unit=input("What unit do you want to use? \n")
if unit.lower() == "kgs":
print(f"{name} is {weight} kgs.")
elif unit.lower()== "lbs":
convert=weight/2.2
print(f"{name} is {convert} lbs.")
else:
print("Please re-enter.")
当我使用.lower()时:我得到了想要的结果
name=input("Insert your name here: \n")
weight=float(input("Enter your weight here: \n"))
unit=input("What unit do you want to use? \n")
if unit == "kgs" or "KGS":
print(f"{name} is {weight} kgs.")
elif unit == "LBS" or "lbs":
convert=weight/2.2
print(f"{name} is {convert} lbs.")
else:
print("Please re-enter.")
name=input("Insert your name here: \n")
weight=float(input("Enter your weight here: \n"))
unit=input("What unit do you want to use? \n")
if unit.lower() == "kgs":
print(f"{name} is {weight} kgs.")
elif unit.lower()== "lbs":
convert=weight/2.2
print(f"{name} is {convert} lbs.")
else:
print("Please re-enter.")
谢谢你的帮助。改变
if unit == "kgs" or "KGS":
到
您必须在整个表达式的或
两侧编写完整的表达式,才能按预期的方式进行计算
不过,在这种情况下使用
lower
更好,因为它还可以捕获用户键入“Kgs”的情况。它与的关系最小。lower()
,您没有在完整表达式中使用单位
变量
更改此项:
if unit == "kgs" or "KGS":
为此(使用或
运算符两侧的装置
):
请看哪个有解释。