0

Can I use variables to take users input as index for a Python list ?

14th Jun 2021, 11:04 AM
malbar3353
malbar3353 - avatar
9 Answers
+ 2
malbar3353 I think Slick meant; choice = int(input("Enter a number (1-5): ")) - 1
14th Jun 2021, 11:59 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Thanks Slick and ChaoticDawg it workedđŸ„łđŸ„łđŸ„ł I learned a new thing today !
14th Jun 2021, 12:39 PM
malbar3353
malbar3353 - avatar
+ 1
Yes ChaoticDawg, thanks
14th Jun 2021, 12:00 PM
Slick
Slick - avatar
0
a = ['a', 'e', 'i', 'o', 'u'] choice = int("Enter a number (1-4): ") - 1 print("You chose:", a[choice]) # input Enter a number (1-4): 2 #output You chose: e
14th Jun 2021, 11:06 AM
Slick
Slick - avatar
0
Slick apparently your code seems not to be working I've got an error message : "invalid literal for int() with base 10 : 'enter a number (1-4):'"
14th Jun 2021, 11:45 AM
malbar3353
malbar3353 - avatar
0
Yeah, you have to enter a number!
14th Jun 2021, 11:58 AM
Slick
Slick - avatar
0
The input doesn't include the prompt. I added that cause that's what you'd see and i thought it'd help a bit
14th Jun 2021, 11:59 AM
Slick
Slick - avatar
0
✌✌
14th Jun 2021, 4:54 PM
MikkyTech
MikkyTech - avatar