I have made a little "game where you guess the number. However I have a problem. I need to be able to restart the loop instead of having the "wrong/right answer" text be repeated.
Here is what I have:number = 34
guess = int(input("guess the number"))
while guess != 34:
if guess > number:
print("Too high")
else:
print("too low")
if guess == number:
print("You got it.")
解决方案You only take input once. You''ll need to update guess (get new input from the user) at the end of your loop if it''s not the correct number.
Pretend you''re playing the game with a friend you''re the "computer" and your friend is the user who has to guess the number you''re thinking of. How will that go?
1) Your freind makes a guess
2) You evaluate that guess, to low/high or correct
3) You repeat 1) and 2) untill the guess is correct
So you''ll now you should be able to realize that you need to take a guess and evaluate it in a loop.
First of all, can''t you see that the algorithm itself is incorrect?
The code will bring the "else" branch of your "if" when the "guess" is not more than 34, so if it is exactly 34, you will print "too low". This is not correct, as in next line you will print "You got it". Not mentioning that you''re using immediate constant of 34 hard-coded in your function. Is the goal of the game to guess 34? :-) Hard-coded constants should always be avoided, except for widely used constants like 0 or 1.
As to your question, you apparently don''t know what the loop is, so it''s hard to guess what you mean by "next line". There is nothing which requires a fragment of code to be repeated. Just read the manual: http://docs.python.org/tutorial/controlflow.html[^].
—SA