pythonuser-input

How to be able to retry an input for a specific key in dictionary in Python without skipping it


I'm trying to make a script that will ask for 5 score input from the user. Each score corresponds to a specific dilution. The input are strings and only six "characters" are allowed to be entered and q to quit the script and show each dilution with their score that have been entered. The problem is that when a invalid "character" is used it skips the dilution completely and does not add it to the dictionary and what I want is to retry the input for the specific dilution.

dil = ["1:16", "1:32", "1:64", "1:128", "1:256"]
corr_input = ["+", "++-", "-", "+-", "-a", "A"]
scores = {}

for dil in dil:
    testscore = input("Enter score: ")
    try:
        if testscore in corr_input:
            scores[dil] = testscore
        elif testscore == "q":
            print("Done!")
            break
        else:
            print("Not a valid score!")
    except TypeError:
        print("Invalid input! Try again")
        break
print(scores)

What I get!

######### 
Enter score: +
Enter score: +
Enter score: v
Not a valid score!
Enter score: +
Enter score: +
{'1:16': '+', '1:32': '+', '1:128': '+', '1:256': '+'}`

Solution

  • The typical way of accomplishing this is with a nested loop. For every dilution, you want a sub-loop that runs until a valid score (or q) is entered.

    dil = ["1:16", "1:32", "1:64", "1:128", "1:256"]
    corr_input = ["+", "++-", "-", "+-", "-a", "A"]
    scores = {}
    
    for dil in dil:
        while dil not in scores:  # This loop will run until the dilution has a score
            testscore = input("Enter score: ")
            try:
                if testscore in corr_input:
                    scores[dil] = testscore
                elif testscore == "q":
                    print("Done!")
                    exit()  # Use exit() to exit the program with 'q'
                else:
                    print("Not a valid score! Try again")
            except TypeError:
                print("Invalid input! Try again")
    
    print(scores)
    

    Note that the above example uses exit() to break out of both loops at once, instantly ending the program. Alternatively, you will need to use a variable like to_exit to break out of the outer for loop, like so:

    dil = ["1:16", "1:32", "1:64", "1:128", "1:256"]
    corr_input = ["+", "++-", "-", "+-", "-a", "A"]
    scores = {}
    
    to_exit = False
    
    for dil in dil:
        while dil not in scores:  # This loop will run until the dilution has a score
            testscore = input("Enter score: ")
            try:
                if testscore in corr_input:
                    scores[dil] = testscore
                elif testscore == "q":
                    print("Done!")
                    to_exit = True
                    break  # This will break out of the while loop but NOT the for loop
                else:
                    print("Not a valid score! Try again")
            except TypeError:
                print("Invalid input! Try again")
        if to_exit:
            break  # This will break out of the for loop
    
    print(scores)