[Python] How to be able to retry an input for a specific key in dictionary in Python without...

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    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': '+'}`

    Continue reading...

Compartilhe esta Página