Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- # calculates multipier based on how large the denominator can get
- def calculate_starting_multiplier(max):
- if max <= 5:
- return 1
- elif max > 5 and max <= 7:
- return 1.25
- elif max > 7 and max <= 9:
- return 1.5
- elif max > 9 and max <= 12:
- return 1.75
- elif max > 12 and max <= 15:
- return 2
- else:
- return 3
- def get_difficulty():
- DIFFICULTIES = {"EASY": 10, "MEDIUM" : 5, "HARD" : 3}
- chances = input(f"Select difficulty(1 - {DIFFICULTIES['EASY']}, 2 - {DIFFICULTIES['MEDIUM']}, 3 - {DIFFICULTIES['HARD']}): ")
- if (chances.isalpha()):
- print("Please enter the number associated with the difficulty (1,2,3)")
- return get_difficulty()
- chances = int(chances)
- if chances == 1:
- return DIFFICULTIES['EASY']
- elif chances == 2:
- return DIFFICULTIES["MEDIUM"]
- elif chances == 3:
- return DIFFICULTIES["HARD"]
- else:
- print("Please enter a 1, 2, 3 to select difficulty")
- return get_difficulty()
- def game():
- print("Fraction Game\nAnswer with the correct fraction rounded to the thousandths place if possible.")
- a = input("What is the minimum denominator: ")
- b = input("What is the maximum denominator: ")
- # swapping the values if they're entered in the wrong order
- if int(a) > int(b):
- temp = a
- a = b
- b = temp
- print(f"a: {a}\nb: {b}")
- # checking to make sure the numbers are indeed numbers
- if a.isalpha() or b.isalpha():
- print("The input you provided is incorrect, make sure to enter only natural numbers.")
- game()
- return
- # once numbers have been confirmed as non-alphabetical, we turn them into natural numbers
- a = abs(int(a))
- b = abs(int(b))
- # points gamify the process
- points = 0
- # correct answers increase the multiplier
- correct = 0
- # multipier value of points
- correct_multiplier = calculate_starting_multiplier(b)
- # chance left until games in
- chances = get_difficulty()
- starting_chances = chances
- while chances > 0:
- # creating this rounds number
- denominator = randrange(a, b + 1)
- if denominator == 1:
- # if denominator is 1 we generate a random number between 1 and the max value as it'll be a natural number
- numerator = randrange(1, b)
- else:
- numerator = randrange(1, denominator)
- correct_answer = str(numerator) if denominator == 1 else str(round(numerator / denominator, 3))
- answer = input(f"{numerator}/{denominator} = ")
- # if correct add points
- if answer == correct_answer:
- # every 5 correct we update the multiplier
- if correct % 5 == 0:
- correct_multiplier += 0.1
- correct += 1
- points += (1 * correct_multiplier)
- print("Good!")
- else:
- chances -= 1
- print(f"{'x' * (starting_chances - chances)}")
- print(f"Final Score: {round(points, 2)}")
- game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement