0

Learning how to create if else statements and struggling. I don't understand the final output. If the answer is "N" then the while raining part should no longer be valid and it should move to the else statement. That does happen, but why is it printing out "stay inside and wait"? That's not part of the else statement?

Raining = input("Is it raining outside? Press Y or N")

if Raining == "N":
    print("Go outside")
    exit
if Raining == "Y":
    print()
    print("Stay inside and wait")
    print()
    while Raining == "Y":
        Raining = input("Is it still raining outside?")
        print()
        print("Stay inside and wait")
    else:
        print("Go outside")
        exit ()

Output:

Is it raining outside? Press Y or NY

Stay inside and wait

Is it still raining outside?Y

Stay inside and wait Is it still raining outside?N

Stay inside and wait Go outside

Hellfire
  • 95
  • 1
  • 6

5 Answers5

0

The else part is chained to while, not if. They're not aligned according to indentations. else for while means different. You should correct this part:

if Raining == "Y":
    print()
    print("Stay inside and wait")
    print()
    while Raining == "Y":
        Raining = input("Is it still raining outside?")
        print()
        print("Stay inside and wait")
else:
    print("Go outside")
    exit ()
F.NiX
  • 1,457
  • 3
  • 12
  • 20
  • And the first `if` can be removed – Cid Oct 06 '20 at 07:09
  • @F.NiX That works for no, but not for yes. Is it raining outside? Press Y or N: Y Stay inside and wait Is it still raining outside?N Stay inside and wait – Hellfire Oct 07 '20 at 08:38
0

Rewrite it as follows.

Raining = input("Is it raining outside? Press Y or N")

while Raining == "Y":
 print("Stay inside and wait")
 print()
 Raining = input("Is it still raining outside?")
 print()
 print("Stay inside and wait")

print("Go outside")
BeeFriedman
  • 1,800
  • 1
  • 11
  • 29
0
Raining = input("Is it raining outside? Press Y or N: ")

if Raining == "N":
    print("Go outside")
    exit
if Raining == "Y":
    while Raining == "Y":
        print()
        print("Stay inside and wait")
        print()
        Raining = input("Is it still raining outside?")
 else:
     print("\nGo outside")
     exit ()
Python2019
  • 33
  • 7
0

Here is my recommendation about your code. You may consider this one too:

Raining = input("Is it raining outside? Press Y or N: ")
while Raining == 'Y':
    print()
    print("Stay inside and wait")
    print()
    Raining = input("Is it still raining outside?")
    if Raining == 'Y':
        print()
        print("Stay inside and wait")
        break
else:
    print("Go outside")

You may break the loop in if/else statement if the first input is 'Y' and then it won't show the else part of while loop.

Saeed
  • 3,255
  • 4
  • 17
  • 36
  • Thanks, but I don't understand. I copied that code, but the yes response doesn’t work? It either dosnt give the correct response or terminates the program after the second yes. Is it raining outside? Press Y or NY Stay inside and wait Is it still raining outside?N Wait for the sake of God :) – Hellfire Oct 07 '20 at 08:48
  • Hi, what's the problem? If you answer Y, you go through the while loop and if you again answer Y, you get `Stay inside...`. If you answer N after being in the loop, you get `Wait for the ...`. Any problems you did not get? – Saeed Oct 08 '20 at 15:20
  • Two problems. If you press Y twice, the program terminates. If you press Y then N, it says wait for the sake of god and then terminates. The only way where that works is if you remove "wait for the sake of god" else statement, then a Y and N will get the correct response. – Hellfire Oct 12 '20 at 02:08
  • Hello, if you get what you want then modify my code to reach your goal. But code is working fine. If you press Y twice, it terminates as expected. You can remove the `else` part of `if` loop, and then move the last `else` part up and indent it. The code is now edited. Check it again please. – Saeed Oct 12 '20 at 08:33
0

Instead of just throwing code at you I am actually going to tell you what's going on, since I haven't seen anyone explaining what's going on.

Let's go step by step.

Raining = input("Is it raining outside? Press Y or N")

After this line your programs pauses execution and waits for an input. You press Y or N. For the sake of this scenario, because you asked this in your question, let's say you pressed Y at this point.

if Raining == "N":
    print("Go outside")
    exit

It doesn't go into this block, because Raining == 'Y'

if Raining == "Y":
    print()
    print("Stay inside and wait")
    print() 
    while Raining == "Y":
        Raining = input("Is it still raining outside?")
        print()
        print("Stay inside and wait")
    else:
        print("\nGo outside")
        exit ()

No here's the thing. Raining == "Y" is true, so it goes into it. The program prints "Stay inside and wait" for the first time, outside the while cycle. Then while Raining == "Y": gets evaluated - it's true, because Raining is still Y, since it hasn't changed yet. It asks for an input in the input("Is it still raining outside?") but then continues executing the rest of the block regardless if you put in Y or N and there is print("Stay inside and wait") two lines below the last input. Only after the block finishes and it cycles back to the while Raining == "Y": it realizes Raining is not Y anymore but has been changed to N so it goes into the else block.

To fix this you might want to put the output in a more appropriate place.

if Raining == "N":
    print("Go outside")
    exit
if Raining == "Y":               
    while Raining == "Y":
        print()
        print("Stay inside and wait")
        Raining = input("Is it still raining outside?")
    else:
        print("Go outside")
        exit ()
Dropout
  • 13,653
  • 10
  • 56
  • 109