Tom Tom - 6 months ago 7
Python Question

Python comparing input with boolean expressions

New to python. I'm writing this code and I can't for the life of me get my program to print the else statement (yourName, 'needs practise') when the input for swim and cycling is a combination of 'Y' and 'N'. what am I doing wrong?

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()

Answer

You can do it this way:

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

Where char is 'y' or '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() converts the string to lowercase.