Question

In this piece of code I am looking for a input:

mode = input("Generate S1(0) or S2(1)?\n")
if mode == "0":
    mode = "S1"
elif mode == "1":
    mode = "S2"
else:
    print("Mode not recogised!")

for being able to handle error better (mode>1), i.e. when if hit the else condition, I want the code to ask the input again.

Any idea how i can do that or which function I am looking for?

Was it helpful?

Solution

mode = None
while not mode:
    answer = input("Generate S1(0) or S2(1)?\n")
    if answer == "0":
        mode = "S1"
    elif answer == "1":
        mode = "S2"
    else:
        print("Mode not recogised!")

OTHER TIPS

This is bit tricky. But you can do like this.

while 1:
    mode = input("Generate S1(0) or S2(1)?\n")
    if mode == "0":
        mode = "S1"
    elif mode == "1":
        mode = "S2"
    else:
        print("Mode not recogised!")
        continue
    break
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top