+ 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")

4th Nov 2022, 1:57 PM
Engineer X
Engineer X - avatar
11 odpowiedzi
+ 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
4th Nov 2022, 3:05 PM
Lothar
Lothar - avatar
+ 3
Ask input again inside loop. If the input is "yes", it will continue, otherwise repeat asking .. No need if-else blocks. Remove those.
4th Nov 2022, 2:18 PM
Jayakrishna 🇮🇳
+ 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")
4th Nov 2022, 3:10 PM
Jayakrishna 🇮🇳
+ 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.
4th Nov 2022, 2:25 PM
Engineer X
Engineer X - avatar
+ 2
Hi Lothar, Thanks for the information, I will give this code a try.
4th Nov 2022, 3:09 PM
Engineer X
Engineer X - avatar
+ 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")
4th Nov 2022, 2:02 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 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")
4th Nov 2022, 2:06 PM
Engineer X
Engineer X - avatar
+ 1
You can try it in Visual Studio Code if you have a computer at the moment.
4th Nov 2022, 2:08 PM
Engineer X
Engineer X - avatar
+ 1
Thanks, I will try this code too.
4th Nov 2022, 3:11 PM
Engineer X
Engineer X - avatar
+ 1
I shall try the same way.
6th Nov 2022, 4:57 AM
Asim Farheen ⭐⭐🤺👿👿
Asim  Farheen ⭐⭐🤺👿👿 - avatar
0
🍁
6th Nov 2022, 11:56 AM
MURUGAN M R
MURUGAN M R - avatar