1

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()
1
  • Yeah. So if both inputs are 'y' then the person will be an athlete. If both are 'n' then they need practise but if one of the inputs is a 'y' and the other is a 'n' then i want the potential statement to show.. Commented Jun 5, 2016 at 18:52

3 Answers 3

1

You need to change the condition as:

 if swim is 'Y' and cycling is 'Y' or swim is 'y' and cycling is 'y':

In python, for the above example, If swim: will mean whether swim exists, which in this case is true.

Sign up to request clarification or add additional context in comments.

1 Comment

Since its an input, it will be string if entered. So False case would not come here. I think so. But thanks, I amde an edit that make the statement specific to the problem.
0

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.

2 Comments

Could you please explain in greater deatial? How would I write that with my variables etc?
Thanks @SilentMonk. Much appreciated
0

You haven't made your desired logic totally clear here, but if goal is to print 'shows potential' if yes to either swims or cycles, 'is an athlete' if both, and 'needs practice' if neither, the below code is a more readable option.

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? ")

    swims = swim and swim.upper() == "Y"
    cycles = cycling and cycling.upper() == "Y"

    if swims and cycles:
        print(yourName, 'is an athlete.')
    elif (swims or cycles):
        print(yourName,'shows potential.')
    else:
        print(yourName,'needs practise')

main()

2 Comments

Thanks Jeremy that's exactly what I was looking for. Thank you
Great @Tom, if this answers your question please accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.