+ 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.
4 Réponses
+ 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>
+ 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.
+ 2
@Lothar
So while loop is the only option?
+ 2
Thanks for the answers people 👍😊.