How to replay code in python
Im creating a program that asks for the user to enter a number from 1-100, the program will tell the user when these numbers are too high or too low, and when they win. When they do win, they are asked if they want to play again or stop. The problem is that I don't know how to get the program to replay the game. Help is greatly appreciated (and I know that most of you will want to use def, but I don't know how to use it, so I would appreciate it if you did not use it) Thanks.
asked Nov 13, 2014 at 1:52 1
zabop 5,7183 gold badges26 silver badges65 bronze badges answered Nov 13, 2014 at 2:12
Alex LAlex L 8,3564 gold badges45 silver badges75 bronze badges
Assuming this code works (I didn't run it) you would wrap it in some kind of loop that executes until broken out of. In this case I used a while loop that tested a boolean called go. Originally it is true, meaning that the while loop will repeat itself over and over again, but when the user wants to stop I handle that by setting go to False. The while loop won't execute because go is now false and your program will end because there is nothing else to execute after the while loop. answered Nov 13, 2014 at 2:09
user3282276user3282276 3,4857 gold badges29 silver badges48 bronze badges 0 How do you go back a loop in Python?The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.
How do you end a program in Python?Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.
|