Skip to content

Instantly share code, notes, and snippets.

@daniel-schroeder-dev
Created August 14, 2022 21:53
Show Gist options
  • Save daniel-schroeder-dev/33ec828a3f6439366e12aa545004d6af to your computer and use it in GitHub Desktop.
Save daniel-schroeder-dev/33ec828a3f6439366e12aa545004d6af to your computer and use it in GitHub Desktop.
Nathany - Math Quiz
from random import randint
def display_welcome_banner():
welcome_banner = """
Welcome to the Math Quiz!
You'll start with three lives.
Each round, you'll be asked to solve a math problem.
If you solve the problem correctly, you won't lose any lives.
If you can't solve the problem, your lose a life.
You can gain lives if you solve a problem correctly
and your lives and level number have the same parity.
Otherwise, your lives stay the same, even if you
solve the problem correctly.
You can play until you're out of lives.
"""
print(welcome_banner)
def check_lives_increase():
pass
def get_operator():
choice = randint(0, 3)
if choice == 0:
return "+"
elif choice == 1:
return "-"
elif choice == 2:
return "*"
elif choice == 3:
return "/"
def is_even(number):
return number % 2 == 0
def is_odd(number):
return number % 2 != 0
def solve(num1, num2, operation):
if operation == "+":
return num1 + num2
elif operation == "-":
return num1 - num2
elif operation == "*":
return num1 * num2
elif operation == "/":
return num1 / num2
def has_parity_match(lives, round_num):
if is_even(lives) and is_even(round_num):
return True
elif is_odd(lives) and is_odd(round_num):
return True
else:
return False
def get_expression(first_term, second_term, operator):
message = ""
if operator == "/":
message = "(round the answer to the nearest whole number)"
return f"What is {first_term} {operator} {second_term} {message}? "
display_welcome_banner()
user_name = input("Enter your name: ")
still_playing = True
round_num = 0
lives = 3
while still_playing:
num1 = randint(1, 10)
num2 = randint(1, 10)
round_num += 1
operator = get_operator()
expression = get_expression(num1, num2, operator)
user_guess = # Prompt the user with the expression
answer = solve(num1, num2, operator)
if user_guess == answer:
print("You are correct!")
print(f"lives: {lives}")
elif user_guess != answer:
print("You are wrong!")
lives -= 1
print(f"lives: {lives}")
elif has_parity_match(lives, round_num):
lives += 1
elif lives == 0:
print("You lost!")
print(f"lives: {lives}")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment