could you check my python code for any mistakes import random #random is used to…
- User
could you check my python code for any mistakes import random #random is used to generate questions import time #all of the time.sleeps in this program is to slow #down the output so that the user has some time to pick up the information
def main(): ''' Description: This function is the main function that will start the whole program this function will call a function based on the users input. Arguments: None Return: None ''' time.sleep(1.5) # A loop to keep the game going while True: choice = display_menu()
if choice == '1': start_quiz() elif choice == '2': game_rules() elif choice == '3': print('Thanks for playing! Goodbye') break else: print('Invalid input!\n' 'Please enter the number that is next to your choice.')def display_menu(): ''' Description: This function will display the main menu where the user has the choise of playing the game or game instructions or exit. Arguments: None Return: (string): the choise that the user has entered. ''' time.sleep(1)
#Display the menu / options print('\nHello user do you want to test your math knowledge?') print('\n \t 1: Start') print('\n \t 2: Game rules') print('\n \t 3: Exit') #Get the choice of the user choice = input('\nPlease enter your choice: ') return choicedef start_quiz(): ''' Discription: This function will run the other functions in order to start the game Arguments: None Return: None
''' dificulty = dificulty_level() user_input, correct_answers = questions(dificulty) result = show_results(user_input, correct_answers)def dificulty_level(): ''' Description: This function lets the user choose the type of difficulty level,(easy, medium and hard). Arguments: None Return: (str): the selected difficulty. '''
time.sleep(.65) #display the dificulty options. print('\nStarting the Game...') time.sleep(3.78 ) print('\nPlease Select The Dificuty Level') print('\n \t 1. Easy') print('\n \t 2. Medium') print('\n \t 3. Hard') dificulty = input('\nEnter your choice: ') #Determine what the user has entered. if dificulty == '1': return 'easy' elif dificulty == '2': return 'medium' elif dificulty == '3': return 'hard' else: print('Invalid Input! Defulting to Easy') return 'easy'def questions(dificulty): ''' Description: This function will pgive the user 10 random questions based on the difficulty level. Arguments: (int) dificulty Return: (int): user intput (answer fot he user) (int): answers
''' user_input = [] correct_answers = [] questions = [] #loops 10 times for 10 questions. for i in range(10):#generates random numbers for the questions. num1 = random.randint (1,100) num2 = random.randint (1,100) operators= () #chooses the operator for the questions based on the difficulty. if dificulty == '1': operators = random.choice (['+', '-']) while operators == '-' and num1 < num2: #this will make sure there are no negative as answers. num2 = random.randint (1,100) elif dificulty == '2': operators = random.choice (['*', '/']) while operators == '-' and num1 < num2: #this will make sure that when there is so a > b num2 = random.randint (1,100) #division there are no decimals for answers. elif dificulty == '3': operators = random.choice(['*', '/', '%', '//']) question = f'{num1} {operators} {num2}' #put the question together. answers = eval (questions) #the eval function will convert the string into mathematical functions. questions.append (question) correct_answers.append (answers) #check if the user has entered a valid input try: user_input = int(input(f'Solve: {question} = ')) #display the equation to the user user_input.append (user_input) except: print('Invalid input, counted as incorrect.') return user_input, correct_answers - Assistant