'Display user choice from list [closed]
Hi I'm writing a program currently I'm pretty new to python,
what I'm wanting is for the user to select from two options like so
1. Easy Mode
2. Hard Mode
Solution 1:[1]
You set Choice = int(...), so Choice is still 1 or 2, so when you print it it'll show up as 1 or 2. Instead, you could set a variable mode depending on which if statement is entered:
Choice = int(input("Select your difficulty: "))
mode = "Invalid choice" # Set a default value
if (Choice == 1):
mode = "Easy Mode"
elif (Choice == 2):
mode = "Hard Mode"
print ("Role: " , mode)
Of course, this doesn't account for the case when the user's input isn't one of these choices, but that's a different question: Asking the user for input until they give a valid response
Solution 2:[2]
Use a dictionary to associate the string the user enters with the one you want to display to them later:
difficulty = {
"1": "Easy Mode",
"2": "Hard Mode",
}
for c, desc in difficulty.items():
print(f"{c}. {desc}")
choice = input("Select your difficulty: ")
while choice not in difficulty:
choice = input(f"Choose one of: {', '.join(difficulty)}: ")
print(f"Role: {difficulty[choice]}")
1. Easy Mode
2. Hard Mode
Select your difficulty: asdf
Choose one of: 1, 2: 2
Role: Hard Mode
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pranav Hosangadi |
| Solution 2 | Samwise |
