gpt4 book ai didi

python - if 语句和多个条件出错

转载 作者:行者123 更新时间:2023-12-01 01:59:25 24 4
gpt4 key购买 nike

我在检查输入是否正确的 if 语句中遇到问题。当我输入M或F时,它会打印出“错误输入”,我不太明白。

def check(aw,iw):
if abs(aw-iw)<5 :
print "Your weight is normal."
elif abs(aw-iw)>5 and abs(aw-iw)<15 :
print "Your weight is about normal."
else:
print "Your weight is not normal."
return

print "Enter your gender (M/F)"
gender=raw_input()
if gender!='M' or gender!='F':
print "Wrong input."
else:
if gender=='M':
w=raw_input("Enter your weight (kg) :")
h=raw_input("Enter your height (cm) :")
idw=110-h
check(w,idw)
else:
w = raw_input("Enter your weight (kg) :")
h = raw_input("Enter your height (cm) :")
idw = 110 - h
check(w, idw)

最佳答案

每个输入要么不等于M,要么不等于F(例如,M不等于F)。相反,您需要检查您的输入是否不等于 M 不等于 F:

if gender != 'M' and gender != 'F':
# Here ------^
print "Wrong input."

或者,更优雅地使用 not in 运算符:

if gender not in ('M', 'F'):
print "Wrong input."

关于python - if 语句和多个条件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49834671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com