0

I made a very simple code to check if a given inputted number was even or odd. I enclosed the entire block of code in a while-loop . At the end of the block, the user is given the choice to input either "Y" or "N". N changes the value of variable i for which the while loop depends on, so it can never run

This was achieved using if: . However, even if anything other than N is pressed, i's value is still somehow changed and doesn't run again. Here is the code

 i = 0
while i == 0:
    number= int(input("Enter your number "))
    if number%2==0:
        print("Your number is even")
    else:
        print("Your number is odd")
        
    askagain= str(input("Do you wanna enter again? Y or N? \n")) 
    if askagain == "N" or "n":                             
        i = 1            
    else:
        i = 0

Even if I input Y , the program never loops back. My only fix was swapping

`if askagain == "N" or "n":                             
            i = 1            
        else:
            i = 0`

with

if askagain == "Y" or "y":                             
            i = 0            
        else:
            i = 1

which somehow mysteriously stopped the issue. What exactly is causing i to iterate itself and change its value from 0, even if "N" was not inputted?

There's only one if clause that can iterate i and change its value in such a way, the while loop command never iterates again.

However, even when the if clause is not executed, somehow, i increments itself and the while loop never executes. What's causing i to increment itself?

Hash
  • 147
  • 1
  • 10
  • `input` always returns a `str`; there's no need to call `str` on the return value. – chepner Sep 16 '20 at 19:35
  • There's also no reason to use an `int` in place of a `bool`, nor is there any need to use a flag at all. Use `while True`, and use `break` where you would have set your flag to `1`/`True`. – chepner Sep 16 '20 at 19:36
  • `bool("n")` gives result `True` – user70 Sep 16 '20 at 19:37
  • @chepner how would bool work in this case? And what's a 1/true flag? – Hash Sep 16 '20 at 19:49
  • @user70 I don't understand. What do you mean exactly? – Hash Sep 16 '20 at 19:50
  • Would it be possible for my question to be reopened by any chance? My question was in relation to a variable involved in a while loop seemingly iterating itself, even when the only condition that could iterate it, was not executed – Hash Sep 16 '20 at 19:53
  • There are two condition in your `if` statement. Think the condition like that: `if (askagain == "N") or ("n")`. When you write like this Python takes`askagain == "N"` as one condition, and takes `"n"` as another condition. So `askagain == "N"` might be `True` or `False`, but `"n"` will be always `True`. It means, your if statements are always `True`. – user70 Sep 16 '20 at 19:55
  • @user70 So what you mean is, python thinks, if "n" is True, execute i = 1? – Hash Sep 16 '20 at 19:56
  • There's no "if"; `"n"` *is* `True`, so it doesn't matter if `askagain == "N"` is true or not. – chepner Sep 16 '20 at 19:58
  • @HankRyan Read the linked link, you'll get it easily. – user70 Sep 16 '20 at 19:59

0 Answers0