import random # check that users have entered a valid option based on a list def string_checker(question, valid_ans=('yes', 'no')): error = f"Please enter a valid option from the following list: {valid_ans}" while True: # get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Inter checker function to validate the user input is a number and is within a number range def int_check(question, low, high, num_type): while True: error = f'Please enter a number between {low} and {high}.' if num_type == "decimal": data_type = float else: data_type = int try: response = data_type(input(question)) if response >= low and response <= high: return response else: print(error) except ValueError: print(error) # Variables difficulty_list = ["easy", "medium", "hard"] easy_difficulty = ["+", "-"] medium_difficulty = ["x", "+", "-"] hard_difficulty = ["x", "÷"] game_history = [] rounds_played = 0 rounds_won = 0 rounds_lost = 0 waiting = 0 rounds_playedV2 = 0 rounds_wonV2 = 0 rounds_lostV2 = 0 game_historyV2 = [] wait = 0 ai = ["Times you've said no.", "'No's!? Just say yes already...", "Times to say no is a lot.", "'No's is what your making me live with.", ". You're wasting power.", "Hurry up, I'm sad."] print() print("Maths Quiz") print() # asks user for number of rounds they want rounds = int_check(f"How many rounds do you want? (3 to 10): ", 3, 10, "integer") print(f"Great, you chose {rounds} rounds.") print() # asks user for what difficulty they want rounds_difficulty = string_checker("What difficulty do you want? (Easy (e), Medium (m), or Hard (h): ", difficulty_list) if rounds_difficulty == "easy": print("If you are 10 years and younger, than easys a good choice. IF YOU'RE NOT THEN CHOOSE SOMETHING DIFFERENT NEXT TIME!") elif rounds_difficulty == "medium": print("Mediums not a bad choice, but maybe go hard next time?") else: print("You want hard? I'll give you hard.") print() # questions for the user starts here and loops until 'round_played' match 'rounds' # in other words, keeps repeating until the users chosen number of rounds are done while rounds_played < rounds: # picks 'easy', 'medium' or 'hard' based on user input # then chooses what the numbers are and then if its '+', '-', or 'x' # based on the difficulty if rounds_difficulty == "easy": num1 = random.randint(1, 12) num2 = random.randint(1, 12) chance = 2 round_variable = random.choice(easy_difficulty) if round_variable == "-": if num1 > num2: answer = num1 - num2 rounded_num = answer else: answer = num2 - num1 rounded_num = answer else: answer = num1 + num2 rounded_num = answer elif rounds_difficulty == "medium": num1 = random.randint(4, 12) num2 = random.randint(4, 12) chance = 2 round_variable = random.choice(medium_difficulty) if round_variable == "x": answer = num1 * num2 rounded_num = answer elif round_variable == "-": if num1 > num2: answer = num1 - num2 rounded_num = answer else: answer = num2 - num1 rounded_num = answer elif round_variable == "+": answer = num1 + num2 rounded_num = answer else: num1 = random.randint(3, 12) * 2 num2 = random.randint(1, 3) * 2 answer = num1 / num2 rounded_num = round(answer, 2) else: num1 = random.randint(6, 15) num2 = random.randint(6, 15) round_variable = random.choice(hard_difficulty) chance = random.randint(1, 1000000000000) if round_variable == "x": answer = num1 * num2 rounded_num = answer else: num1 = random.randint(3, 7) * 2 num2 = random.randint(1, 3) * 2 answer = num1 / num2 rounded_num = round(answer, 2) # asks user question with the high number displayed first and low number last if chance == 1: answer = 1 print(f"Round {rounds_played + 1} of {rounds}") user_answer = int_check('''If you take the product of the square root of fifty minus twenty times the square root of six, the square root of eight plus four times the square root of three, and the square root of twenty‑seven minus eighteen times the square root of two, and then divide each of those three square roots respectively by the difference between the square root of two and the square root of three, the difference between the square root of six and the square root of two, and the difference between the square root of three and the square root of six, what number do you get? ''', 0, 1000000, "decimal") else: if num1 > num2: print(f"Round {rounds_played + 1} of {rounds}") user_answer = int_check(f"What is {num1} {round_variable} {num2}? ", 0, 500, "decimal") else: print(f"Round {rounds_played + 1} of {rounds}") user_answer = int_check(f"What is {num2} {round_variable} {num1}? ", 0, 500, "decimal") if rounded_num == user_answer: rounds_won += 1 result_display = "✅ Woohoo! Correct answer ✅" print(result_display) print() else: rounds_lost += 1 result_display = f"❌ Incorrect. The correct answer was {rounded_num} ❌" print(result_display) print() if chance == 1: history_item = f'''Round {rounds_played + 1}: If you take the product of the square root of fifty minus twenty times the square root of six, the square root of eight plus four times the square root of three, and the square root of twenty‑seven minus eighteen times the square root of two, and then divide each of those three square roots respectively by the difference between the square root of two and the square root of three, the difference between the square root of six and the square root of two, and the difference between the square root of three and the square root of six, what number do you get? = {user_answer} - {result_display} ''' else: if num1 > num2: history_item = f"Round {rounds_played + 1}: {num1} {round_variable} {num2} = {user_answer} - {result_display}" else: history_item = f"Round {rounds_played + 1}: {num2} {round_variable} {num1} = {user_answer} - {result_display}" game_history.append(history_item) rounds_played += 1 if rounds_played > 0: # calculate statistics percent_won = rounds_won / rounds_played * 100 percent_lost = rounds_lost / rounds_played * 100 # output game Statistics print("Game Statistics") print(f"Won: {percent_won:.2f}%\t " f"Lost: {percent_lost:.2f}%\t ") # ask user if they want to see their game history and output if requested. see_history = string_checker("\nDo you want to see your game history? (yes / no): ") if see_history == "yes": for item in game_history: print(item) # end of program print() print("Thanks for playing!") print("Right. On to the next bit.") no_point = string_checker("\nWhat do you think of more fun? ") if no_point == "yes": print("Ok.") else: print("Too bad, we're doing it anyway.") print() roundsV2 = int_check(f"How many rounds do you want this time? (3 to 10): ", 3, 10, "integer") print(f"Okay, you chose {roundsV2} rounds this time.") print() ready = string_checker("Are you ready? ") while ready == "no": if wait > 30: if ready == "Yes": print("Good, lets go.") else: print("I'm waiting. >:(") print() change = random.choice(ai) long = f"{wait + 1} {change}" print(long) ready = string_checker("Are you ready? ") wait += 1 waiting += 1 if waiting == 100: print("I'VE BEEN WAITING FOR ONE 'YES' OUT OF 100 'NO'S!") ready = "yes" else: if ready == "Yes": print("Good, lets go.") else: print("I'm waiting. >:(") print() ready = string_checker("Are you ready? ") wait += 1 waiting += 1 if waiting == 100: print("I'VE BEEN WAITING FOR ONE 'YES' OUT OF 100 'NO'S!") ready = "yes" if ready == "yes": if waiting < 10: print("You took your time. Now. Lets do this") elif waiting < 50: print("What. Were. You. Doing.") elif waiting == 100: print("RIGHT. I'VE HAD ENOUGH! WE'RE START NOW!!!") print() while rounds_playedV2 < roundsV2: num3 = random.randint(1, 10) num4 = random.randint(1, 10) num5 = random.randint(1, 10) num6 = random.randint(1, 10) if num3 > num5: answerV2 = num3 * num4 + num5 - num6 else: answerV2 = num5 * num4 + num3 - num6 if waiting == 100: answerV2 = 172 user_answerV2 = int_check("Right you. Answer this. nali32uy4723547im 9864t 8m048r m3456ds5s3 ", 0, 1000, "decimal") else: if num3 > num4: print(f"Round {rounds_playedV2 + 1} of {roundsV2}") user_answerV2 = int_check(f"What is {num3} x {num4} + {num5} - {num6}? ", 0, 500, "decimal") else: print(f"Round {rounds_playedV2 + 1} of {roundsV2}") user_answerV2 = int_check(f"What is {num5} x {num4} + {num3} - {num6}? ", 0, 500, "decimal") if waiting == 100: if user_answerV2 == answerV2: rounds_wonV2 += 1 result_display = "YOU CHEATED!" print(result_display) print() else: rounds_lostV2 += 1 result_display = "HA HA HA. I BEAT YOU!" print(result_display) print() else: if answerV2 == user_answerV2: rounds_wonV2 += 1 result_display = "✅ Woohoo! Correct answer ✅" print(result_display) print() else: rounds_lostV2 += 1 result_display = f"❌ Incorrect. The correct answer was {answerV2} ❌" print(result_display) print() if waiting == 100: history_item = f"Round {rounds_playedV2 + 1}: nali32uy4723547im 9864t 8m048r m3456ds5s3 = {user_answerV2} - {result_display}" else: if num3 > num4: history_item = f"Round {rounds_playedV2 + 1}: {num3} x {num4} + {num5} - {num6} = {user_answerV2} - {result_display}" else: history_item = f"Round {rounds_playedV2 + 1}: {num5} x {num4} + {num3} - {num6} = {user_answerV2} - {result_display}" game_historyV2.append(history_item) rounds_playedV2 += 1 if rounds_played > 0: # calculate statistics percent_wonV2 = rounds_wonV2 / rounds_playedV2 * 100 percent_lostV2 = rounds_lostV2 / rounds_playedV2 * 100 # output game Statistics print("Game Statistics") print(f"Won: {percent_wonV2:.2f}%\t " f"Lost: {percent_lostV2:.2f}%\t ") # ask user if they want to see their game history and output if requested. see_historyV2 = string_checker("\nDo you want to see your game history? (yes / no): ") if see_historyV2 == "yes": for item in game_historyV2: print(item) if waiting == 100: print() print("DON'T YOU COME BACK.") else: print() print("I hope you had a great time. Thanks for playing!") print("The program has ended")