我在检查输入是否正确的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/

10-10 08:03