Home > Enterprise >  Python dictionary; receive user input; enter error code when out of range
Python dictionary; receive user input; enter error code when out of range

Time:11-01

I am making a dictionary! I am trying to get the user to select a number between 1 - 10 to generate the Korean word for it. I am able to get the user input to print out the correct translation but I want my code to tell the user to try again if a number between 1 - 10 is not selected. Heeelp.

#1 is key
#all keys need to be unique
#Hana is value
print ("Welcome to the Korean Converter!")

user = int(input("Enter a number between 1 and 10: "))
        
koreanConversion = {
    1: "Hana",
    2: "Tul",
    3: "Set",
    4: "Net",
    5: "Ta Sut",
    6: "Yuh Sut",
    7: "Il Jop",
    8: "Yu Dulb",
    9: "Ah Hop",
   10: "Yul"

}


print (koreanConversion[user])


#else: 
 #   user = int(input("Please try again: "))
 #if (koreanConversion[user]) != [user]
  #   print("Please try again.")

CodePudding user response:

Using [] is not usually a good idea when your input has some uncovered cases so use .get method for dictionaries:

print(koreanConversion.get(user, ""))

this means that get the item with user key if it does not exists print "" which is empty, this should fix your problem

CodePudding user response:

Check this


koreanConversion = {
    1: "Hana",
    2: "Tul",
    3: "Set",
    4: "Net",
    5: "Ta Sut",
    6: "Yuh Sut",
    7: "Il Jop",
    8: "Yu Dulb",
    9: "Ah Hop",
    10: "Yul"
}


print("Welcome to the Korean Converter!")

while True:
    _input = input("Enter a number between 1 and 10: ")
    number = int(_input)
    if koreanConversion.get(number, None):
        print(koreanConversion[number])
        break
    else:
        print('Try again.')
        continue

CodePudding user response:

I suggest you try and play with lists:

def KoreanConverter():
    koreanConversion = ["Hana", "Tul", "Set", "Net", "Ta Sut", "Yuh Sut", "Il Jop", "Yu Dulb", "Ah Hop", "Yul"]

    print("Welcome to the Korean Converter!")
    i = int(input("Enter a number between 1 and 10: ")) -1
    if not i in range(0, 9):
        return "Please provide a valid number between 1 and 10!"
    
    return koreanConversion[i]
        
playing = True
while playing: 
    print(KoreanConverter())
    playing = ("y" == input("Keep converting ? y/N")

CodePudding user response:

A couple of people have suggested using .get() with a default and/or checking ahead of time to see if the item is in the dictionary. Another option is to use try/except:

while True:
    try:
        print(koreanConversion[
            int(input("Enter a number between 1 and 10: "))
        ])
        break
    except (KeyError, ValueError):
        print('Try again.')

A nice aspect of using exception handling this way is that you can handle two kinds of exception in one place:

  • ValueError gets raised if the int() conversion fails
  • KeyError gets raised if it's a valid int but not in your dictionary
  • Related