+ 3
How can I change the while loop from infinite to only 1 at a time?
I have this code which asks if is it raining or not. If it is raining, it must say "wait a while" and must asks again this question "is it raining?". And if not, it must say "Go outside". But the problem is the loop is infinite and asks infinitly when I type "yes". How can I stop that and make it to ask one at a time? print("is it raining?") x = input() if x == "yes": while x == "yes": print("wait a while") print("is it raining?") else : print("Go outside")
11 Answers
+ 10
Engineer X ,
this is the structure for the code flow:
> we should not have 2 input() functions
> the while loop should contain all the other code
> to get inside the while loop, we can create the variable `x` outside of the loop. it should be initialized with the string `yes`
see to code versions:
https://code.sololearn.com/ca3baw6yJY7W/?ref=app
+ 3
Ask input again inside loop. If the input is "yes", it will continue, otherwise repeat asking .. No need if-else blocks. Remove those.
+ 3
print("is it raining?")
x = input()
while x == "yes":
print("wait a while")
print("is it raining?")
x = input()
print("Go outside")
# edit: Engineer X better way:
while True:
print("is it raining?")
x = input()
print(x)
if x != "yes" : break
print("wait a while")
print("Go outside")
+ 2
Hi Jayakrishnain,
Like this:
x = input()
while x == "yes":
print("wait a while")
if x == "no":
print("Go outside")
But it asks infinitly
I want it to ask 1 time when I say "yes" and ask again not telling me "wait a while like ustoppable.
+ 2
Hi Lothar,
Thanks for the information, I will give this code a try.
+ 1
Use 'break' keyword, maybe it's helpful for you.
print("is it raining?")
x = input()
if x == "yes":
while x == "yes":
print("wait a while")
x *= 1
break
else :
print("Go outside")
+ 1
Hi Sakshi,
The thing is that I want the computer to ask infinitly if it is raining or not and if it is raining it must say wait a while and ask again until I reply "no" and it must say "Go outside".
I changed the code Btw.
print("is it raining?")
x = input()
if x == "yes":
while x == "yes":
print("wait a while")
print("is it raining?")
else :
print("Go outside")
+ 1
You can try it in Visual Studio Code if you have a computer at the moment.
+ 1
Thanks,
I will try this code too.
+ 1
I shall try the same way.
0
🍁