3

I'm not exactly sure why but when I execute this section of code nothing happens.

while (True) :

    choice = str(input("Do you want to draw a spirograph? (Y/N) "))

    if choice == 'n' or 'N' :
        break

    elif choice == 'y' or 'Y' :    

       <CODE>

    else :
        print("Please enter a valid command.")
        choice = input("Do you want to draw a spirograph? (Y/N)")           
5
  • 2
    Edit your code section and add while line to it. I don't do this myself cause your problem may rely on it. Commented Oct 7, 2014 at 4:52
  • So many SO questions would be avoided in Python self-linted this case.. Commented Oct 7, 2014 at 4:59
  • FWIW, you don't need parenthesis around your predicate. while True: is better. Commented Oct 7, 2014 at 5:00
  • 1
    possible duplicate of How do I test one variable against multiple values? Commented Oct 7, 2014 at 5:02
  • (And see the many linked answers on the duplicate such as stackoverflow.com/questions/20002503/…) Commented Oct 7, 2014 at 5:03

2 Answers 2

12

It won't work because the 'N' literal always evaluates to True within your if statement.

Your if condition currently stands as if choice == 'n' or 'N' :, which is equivalent to if (choice == 'n') or ('N'), which will always evaluate to True irrespective of the value of variable choice, since the literal 'N' always evaluates to True.

Instead, use one of the following

  • if choice == 'n' or choice == 'N' :
  • if choice in 'nN' :
  • if choice in ('n', 'N') :

The same holds for your elif block as well. You can read more about Truth Value testing here.

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

Comments

5

This expression doesn't do what you want:

choice == 'n' or 'N'

It is interpretted as this:

(choice == 'n') or 'N'

You might want to try this instead:

choice in 'nN'

Comments

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.