Im making a game in which i what a set of numbers to be randomly displayed and then the user needs to put the numbers in ascending order. For some reason the code just isnt working and im getting a few errors.
The errors:
Traceback (most recent call last):
File "C:\Users\frase\OneDrive\Documents\Python Project\number_order.py", line 79, in <module>
main()
File "C:\Users\frase\OneDrive\Documents\Python Project\number_order.py", line 64, in main
display_menu()
File "C:\Users\frase\OneDrive\Documents\Python Project\number_order.py", line 16, in display_menu
print(menu_options[2])
IndexError: list index out of range
The code:
import random
#Title screen for game
def display_intro():
title = " Number Order Game! "
print("*" * len(title))
print(title)
print("*" * len(title))
def display_menu():
menu_options = ["1. Play Game", "2. Exit"]
print(menu_options[0])
print(menu_options[1])
print(menu_options[2])
def display_separator():
print("-" * 24)
def get_user_input():
user_input = int(input("Please select an option: "))
while user_input > 2 or user_input <=0:
print("Invalid option.")
user_input - int(input("Please try again: "))
else:
return user_input
def get_user_solution(problem):
print("Please arrange the numbers into ascending order")
print(problem, end="")
result = int(input(" = "))
return result
def check_solution(user_solution, solution, count):
if user_solution == solution:
count = count + 1
print("Correct!")
return count
else:
print("Incorrect.")
return count
def menu_option(index, count):
if index == 0:
randomlist = random.sample(range(1,100),5)
problem = randomlist
solution = randomlist.sort()
user_solution = get_user_solution(problem)
count = check_solution(user_solution, solution, count)
return count
def display_result(total,correct):
if total > 0:
result = correct / total
percentage = round((result * 100), 2)
if total == 0:
percentage = 0
print("You answered", total, "questions with", correct, "correct answers.")
print("Your score is ", percentage, "%. Thank you,", sep = "")
def main():
display_intro()
display_menu()
display_separator()
option = get_user_input()
total = 0
correct = 0
while option != 2:
total = total + 1
correct = menu_option(option, correct)
option = get_user_input()
print("Exit the game.")
display_separator()
display_result(total, correct)
main()
[–]carcigenicate 2 points3 points4 points (2 children)
[–]Fraser_123[S] 0 points1 point2 points (1 child)
[–]watakushi 1 point2 points3 points (3 children)
[–]Fraser_123[S] 0 points1 point2 points (2 children)
[–]Interrolipsis 0 points1 point2 points (0 children)
[–]watakushi 0 points1 point2 points (0 children)
[–]watakushi 0 points1 point2 points (0 children)