+ 2
Hi I have an issue with this code
#installed games games = [ 'Soccer', 'Tic Tac Toe', 'Snake', 'Puzzle', 'Rally'] #taking player's choice as a number input choice = int(input()) #output the corresponding game
1 Answer
+ 3
This is from Introduction to Python -> Working With Lists -> Game Machine.
Up until this point, the lesson has shown you examples of indexing into a list by using a fixed constant integer number. For example:
devices = ["TV", "Notebook", "PC"]
print(devices[1]) # index 1 is "Notebook"
The exercise is asking you to use a variable as an index instead of a constant number. Example:
devices = ["TV", "Notebook", "PC"]
num = 1
print(devices[num]) # prints the same result as above
The code in the exercise defines a list of games. Then it prompts the user to type in a game number. It stores the integer value of the input into a variable named choice. Your job is to write a line of code that uses the choice variable as an index into the games list and print out the name of the selected game.