2

When I type "no" into the input I expected it to add 1 to "x", therefore ending the loop, but what happens is that it ignores it and does not add 1 x. Here is the code.

x = 1
password = ""

while x == 1:        
    # imagine there is some code here which works

    ans1 = input("\n\nTest a new password? ")
    ans1 = ans1.upper()

    print(ans1)

    if ans1 == ("Y" or "YES"):
        x = x
    elif ans1 == ("N" or "NO"):
        x = x + 10

    print(x)

It's the bottom if/elif statement that is not working. It should continue to ask for input again until the user says NO but this isn't working.

2
  • Your if-branch does nothing so you can throw it out. And you can break a loop with 'break'. Commented Jun 4, 2016 at 20:42
  • I liked that previous title. Added some 'mystery' to the question ;) Commented Jun 4, 2016 at 20:46

4 Answers 4

6

You should use or that way.

if ans1 == ("Y" or "YES"):

Can be replaced with:

if ans1 == "Y" or ans1 == "YES": 

Or:

if ans1 in ("Y", "YES"): 

The bug comes from the definition of the or operator. When you do "Y" or "YES", it will return "Y" as A or B is defined to return A if A is not false. Here, A is "Y" which is not a False value. So, it will return A="Y". If you do if a == ("Y" or "YES"):, il will be equivalent to if a == "Y":. Ok it's a bit tricky but it's how python works.

Moreover, your code is very strange. It's a very bad habit to exit a loop like that. Generally, we put a boolean value "looping" that is set to false when we want to leave the loop.

Here's how I would do your loop:

looping = True 
password = "" 

while looping: 

    ans1 = input("\n\nTest a new password? ")

    if ans1.upper() in ("NO", "N"): 
        looping = False

You can also use a construction with an infinite loop (while True:). Then, you call the instruction break to quit the loop.

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

7 Comments

What's the point in the looping variable? Why not while True: and break? I wouldn't say your solution is 'typical Python'
There is a mistake in your answer. A or B will return A not if B is false, but if A is not false. Also, while True: and break are a good solution, mainly in Python, and it's completely acceptable.
I fixed my mistake. Thank you. For the break statement, my teachers always taught me not to use it too often so I never use it. But, I think in that case, it's totally acceptable. It's a very discussed point where to use and not to use break instructions.
ok all I had to do was change the if else statement and it worked. ty I spent ages. also your way of looping is very interesting I may use that :D ty once again
@AlexisClarembeau My teacher tells us to never use 'break', but who really listens? An awesome command that is not used it a shame, isn't it? :)
|
2

You could also use "break" or "exit" to go out of the loop or the program. It's also generally better to use a larger condition that goes well in unexpected case (x<=0 or ans1 isn't YES rather than x==0 or ans1 is YES or ans1 is NO).

while True:
  # Code
  if ans1 not in ["Y", "YES"]:
    break # or exit

Then you would have no undefined behavior, and also fewer condition to take care of : if it isn't "YES" or "Y", the program exit.

Comments

1

Well, there is a problem with this line: ans1 == ("Y" or "YES")

It is not similar to this line:

ans1 == "Y" or ans1 == "YES"

The second one is right, the first one is called null coalescing. That's not what you want. Basically, the idiom x or y returns x if x is not null, otherwise it returns y.

So basically you just check if ans1 is "Y" (not "YES")

You can check if a list of idioms contains yours this way:

if ans1 in ["Y", "YES"]:

And you can continue adding values to that list as many as you want.

Comments

0

You can use list and in statement:

x = 1
password = ""

while x == 1:        
# imagine there is some code here which works

 ans1 = input("\n\nTest a new password? ")
 ans1 = ans1.upper()

 print(ans1)

 if ans1 in ["Y","YES"]:
    x = x
 elif ans1 in ["N","NO"]:
    x = x + 10

print(x)

This ensures that the code works.

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.