How to re execute a input command? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to re execute a input command?

Of course this is not gonna happen on solo learn but, How can we execute an input command that the user has already executed earlier while running the program. Like ans = Input("option: ") Print (ans) Input("Re execute? : ") In the above code, how can I re execute the first line once user gave the input, got a desired outcome and asks again without switching off the program. Thanks for reading.

28th Mar 2020, 6:44 PM
Mirza Ishan Beg
Mirza Ishan Beg - avatar
4 Answers
+ 4
You can put your code in a while loop. But you need to have an exit condition. Keep also in mind, that such a code does not really work in playground, as you have to do all inputs at the start of the program. Could be something like this (pseudo) code: while True: ans = input("option: ") print (ans) ans2 = input("Re execute? : ") if ans2 <a condition>: <do something> else: <do something>
28th Mar 2020, 8:13 PM
Lothar
Lothar - avatar
+ 3
As you don‘t know how often the code will be repeated / executed, there is no other way. But to handle a while is not difficult.
29th Mar 2020, 12:03 PM
Lothar
Lothar - avatar
+ 2
@Lothar So while loop is the only option?
29th Mar 2020, 3:49 AM
Mirza Ishan Beg
Mirza Ishan Beg - avatar
+ 2
Thanks for the answers people 👍😊.
29th Mar 2020, 4:22 PM
Mirza Ishan Beg
Mirza Ishan Beg - avatar