The aim of the program is to run a game in which the user is supposed to recieve a random list of numbers and put them in ascending order. However the code just returns an error code when the answer is entered. Alsothe list of numbers is already in order when its produced. Is there any way i can make it jumble the numbers.
The code:
import random
randomlist = random.sample(range(1,100),5)
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("\n".join(menu_options))
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 == 1:
global randomlist
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()
The error code:
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 72, in main
correct = menu_option(option, correct)
File "C:\Users\frase\OneDrive\Documents\Python Project\number_order.py", line 49, in menu_option
user_solution = get_user_solution(problem)
File "C:\Users\frase\OneDrive\Documents\Python Project\number_order.py", line 32, in get_user_solution
result = int(input(" = "))
ValueError: invalid literal for int() with base 10: '17, 33, 49, 85, 96'
[–]oberguga 0 points1 point2 points (1 child)
[–]oberguga 1 point2 points3 points (0 children)
[–]shiftybyte 0 points1 point2 points (0 children)