Clauric
Clauric

Reputation: 1886

str.isdigit() causing program to hang

I am trying to use str.isdigit() to try and determine a user input. However, when a user inputs a numeric value between the required parameters, the code just hangs. When other inputs are entered, outside of the parameters, the code functions correctly.

The code is:

Input_Value = input("Please choose which activity would would like to run:  ")

while True:

    if Input_Value == "8" or Input_Value == "9" or Input_Value == "0":
        Input_Value = input("Please input an 'x' or a number between 1 & 7: ")

    elif len(Input_Value) > 1:
        Input_Value = input("Please input an 'x' or a number between 1 & 7: ")

    elif (Input_Value.isdigit()) == True:
        Choice = int(Input_Value)
        continue

    elif Input_Value == "x" or Input_Value == "X":
        print()
        print("Thank you for taking part. Good bye")
        print()
        exit()

    else:
        Input_Value = input("Please input an 'x' or a number between 1 & 7: ")

if Choice == 1:
    # do something here

elif Choice == 2:

No errors appear, and the code worked fine (without catching errors) before I put in the elif (input_Value.isdigit() == True:.

Upvotes: 0

Views: 64

Answers (2)

Sh3mm
Sh3mm

Reputation: 58

You put the input outside the loop and you have a continue inside the infinite loop. Because of this, the value of Input_Value never changes and always does the same thing over and over and over...

Since you also have code outside the loop, I suggest you try breakinstead of continue

you might also want to try using:

if Input_Value in ["8", "9", "0"]:

Upvotes: 1

Chemistree
Chemistree

Reputation: 432

The continue you put in the elif branch causes the next iteration of the while True loop to be executed. Since nothin changed, the elif branch will be selected again, the continue will skip to the next iteration... Repeating forever, not allowing any other code to execute.

You might be looking for a break instead

Upvotes: 4

Related Questions