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
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 ()
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")
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 ()
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.
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 ()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.