How to make If statement in loop only run once
i have three if statements and when i write a number it runs infinitetly even tho its if
24 Replies
its your
while
statement that is running infinitelyok so how do i make it so it doesnt run infinitly but the code doesnt end after one input
you need to put everything that you want to repeat inside the while loop
right now your loop runs forever because the value of
on
never changes inside itwell you've set the condition for it to run
while (on < 1)
so you need to make that condition false once you're finishedok
but like
like @jimmacle said as well all the relevant code needs to be inside your while loop as well (i.e. where you're getting the guess from the user)
i want it to run once every time i write something
but you want to write something multiple times?
yes i wanna make a guessing game
if you only want something to run once just don't put it in its own loop
put the entire process that you want to repeat in there
so if you want to guess numbers until you get it right, you probably want your loop to include asking for guesses
what_
i dont understand
which part is confusing?
if the user's guess is wrong you need to ask them for another number?
if the users guess is wrong it says higher or lower depending on if the correct number is higher or lower
right, then what?
and u just do that until you guess the number
but it keeps saying higher many times
do that until you guess the number"that" is what should be in the loop right, walk through your code and see if you can figure out why that's happening
its running the same thing multiple times but idk how to make it run only once
but you don't want it to only run once, you're just looping around the wrong things
you want something to run until they guess the right number, right?
yes
so all those things should be put in the loop
so i add more of the code to the loop??
yes, think about what you need to repeat
you ask me to guess a number
I give you a number
you tell me it's wrong
then...?
aight
thank you
fixed it