python新手。我正在编写此代码,但是当我游泳和骑自行车的输入是“ Y”和“ N”的组合时,我就无法让我的程序打印出else语句(yourName,“需要实践”) 。我究竟做错了什么?

def main():

    yourName = input("What is the your name? ")
    swim = input("Can you swim <Y>es or <N>o? ")
    cycling = input("Can you cycle <Y>es  or <N>o? ")

    if swim and cycling is 'Y' or swim and cycling is 'y':
            print(yourName, 'is an athlete.')
    elif swim and cycling is 'N' or swim and cycling is 'n':
        print(yourName,'shows potential.')
    else:
        print(yourName,'needs practise')

main()

最佳答案

您可以这样操作:

if swim.lower() == <char> <conditional operator> cycling.lower() == <char> :

其中char是'y'或'n'。

def main():

    yourName = input("What is the your name? ")
    swim = input("Can you swim <Y>es or <N>o? ")
    cycling = input("Can you cycle <Y>es  or <N>o? ")

    is_swim = swim.lower()
    is_cycle = cycling.lower()

    if is_swim == 'y' and is_swim == 'y':
            print(yourName, 'is an athlete.')
    elif is_swim == 'y' or is_cycle == 'y':
        print(yourName,'shows potential.')
    else:
        print(yourName,'needs practise')

main()


str.lower()将字符串转换为小写。

09-25 19:59