'flow control - automate the boring stuff (is it raining)
I have looked at this so many times and confused myself.
Looking at the flow chart:
I am getting confused on how to do the loop on the right hand side. This how I "think" it looks.
- Is it raining? (Y) - Have umbrella? (Y) - Go outside.
- Is it raining? (Y) - Have umbrella? (N) - wait a while ... is it raining (Y) ... wait a while ... is it raining etc etc
- Is it raining? (Y) - Have umbrella? (N) - wait a while ... is it raining (N) - Go outside
- Is it raining (N) - Go outside.
This is how I have implemented so far...but after wait a while...and yes to raining, my code goes back to asks me if I have an umbrella.
Where am I going wrong?
import time
while True:
r = input("is it raining? y/n: ")
if r == "y":
u = input("do you have an umbrella? y/n: ")
if u == "y":
print("Go Outside")
break
if u == "n":
print("wait a while")
time.sleep(3)
r = True
else:
print("Go Outside")
break
Solution 1:[1]
There's no need for a global loop, the question "Have umbrella?", for example, is only asked once. The following code should do the trick:
import time
r = input("is it raining? y/n: ")
if r == "y":
u = input("do you have an umbrella? y/n: ")
if u == "n":
while r == "y":
print("wait a while")
time.sleep(3)
r = input("is it raining? y/n: ")
print("Go Outside")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | joao |