Can someone please help me with the following solution? I’m relatively new to programming and I don’t seem to get what I’m doing wrong.
I have found too many dictionary functions and I am really struggling with this. My code give me Key-Error upon Key-Error and when I finally manage to run the program it returns “None” or 0.
Here is an example of the code.
# Dictionary called choices.
choices = {
‘1.’: ’One’,
’2.’: ’Two’,
’3.’: ’Three’,
}
# Display the choices to the user.
for k, v in choices.items():
print(f”{k} {v}”)
# Ask the user to make a choice from the dictionary options.
my_choice = int(input(“Choose a number 1, 2 or 3: “))
# Make the choice the same as the index[number] of the dictionary
my_choice -= 1
# From here I get stuck
print(f”You have chosen {choices.get(str(my_choice))}.”) # This returns a 0 or None
# The desired output is
You have chosen Two.
I have tried the following options to assign dictionary items to the my_choice variable:
for loops
dict.values()
dict .get()
dict.items()
new_choice = str(my_choice)
# The desired output is
You have chosen Two.
1