1
again = True

while(again == True):

    yn = input("Do you want to do it again? (y or n)").lower()

    if(yn == 'y' or 'yes'):
        again == True
    elif(yn == 'n' or 'no):
        again == False

How do I make this code work? When I run it, it will always come out to be true.. =/

1
  • 1
    Note that on the second to last line, you are missing a quote. elif(yn == 'n' or 'no'): Commented Oct 3, 2013 at 3:15

6 Answers 6

6

Let's start from the beginning:

while(again == True):

  1. You don't need the parentheses here

  2. You don't need the == True bit. again is suffice as it will have a boolean value of True.

if(yn == 'y' or 'yes'):

  1. Once again, parenthesis aren't needed.

  2. That line of code is translated as if (yn == 'y') or ('yes'):. This will always be true as 'yes' is considered True (not an empty string)

  3. In python, you need to do if yn == 'y' or yn == 'yes':

  4. Or you can do if yn in ('y', 'yes')

again == True

  1. == is for comparisons. = is for assignments

  2. This occurs in your else block.

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

Comments

1

In the final 4 lines of your program you aren't assigning again to have the value true, you are checking if again equals True/False.

== is completely different from =.

Comments

1

This won't work:

if (yn == 'y' or 'yes'):
    # ...
elif(yn == 'n' or 'no):

The correct way is:

if yn in ('y' or 'yes'):
    # ...
elif yn in ('n' or 'no):

Also, inside the conditions the assignments are wrong, use a single =. They should look like this:

again = True
# ...
again = False

2 Comments

Okay this works now, but when I enter something else other than no it will repeat itself. How do I make it only respond to yes?
@user2840982 then add another condition in an else: to take this case in consideration, for instance, ask the user to enter a correct input. Or do as @JasonSundram suggests, and replace the whole elif line with an else:
0

You are mistaking the equality operator == with the assignment operator =. Also the statement yn == 'y' or 'yes' is buggy, since the right hand side is a non-empty string, which evaluates to True in a boolean context.

Declaring a variable simply to handle loop termination is not pythonic. I would rewrite your code entirely to be:

while True:
    yn = input("Do you want to do it again? (y or n)").lower()
    if yn in ('n','no'):
        break

Comments

0

if(yn == 'y' or 'yes'): # ----

The value of above statement is always true independent of valu of “yn” because right side of “or” is always true in your case. so it will never execute else part. So to avoid this, You have to compare value of “yn” with each possibility. one of the simple way is that

if(yn == 'y' or yn == 'yes'):
    again == True
elif(yn == 'n' or yn == 'no):
    again == False

Comments

-1

your if else statment should be

if xxx: again = True elif yyy: again = False

Note difference between = and ==.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.