Advertisement
BenTibnam

Fraction Game

Aug 16th, 2023 (edited)
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | Gaming | 0 0
  1. from random import randrange
  2.  
  3. # calculates multipier based on how large the denominator can get
  4. def calculate_starting_multiplier(max):
  5.     if max <= 5:
  6.         return 1
  7.     elif max > 5 and max <= 7:
  8.         return 1.25
  9.     elif max > 7 and max <= 9:
  10.         return 1.5
  11.     elif max > 9 and max <= 12:
  12.         return 1.75
  13.     elif max > 12 and max <= 15:
  14.         return 2
  15.     else:
  16.         return 3
  17. def get_difficulty():
  18.     DIFFICULTIES = {"EASY": 10, "MEDIUM" : 5, "HARD" : 3}
  19.     chances = input(f"Select difficulty(1 - {DIFFICULTIES['EASY']}, 2 - {DIFFICULTIES['MEDIUM']}, 3 - {DIFFICULTIES['HARD']}): ")
  20.     if (chances.isalpha()):
  21.         print("Please enter the number associated with the difficulty (1,2,3)")
  22.         return get_difficulty()
  23.    
  24.     chances = int(chances)
  25.  
  26.     if chances == 1:
  27.         return DIFFICULTIES['EASY']
  28.     elif chances == 2:
  29.         return DIFFICULTIES["MEDIUM"]
  30.     elif chances == 3:
  31.         return DIFFICULTIES["HARD"]
  32.     else:
  33.         print("Please enter a 1, 2, 3 to select difficulty")
  34.         return get_difficulty()
  35.  
  36. def game():
  37.     print("Fraction Game\nAnswer with the correct fraction rounded to the thousandths place if possible.")
  38.     a = input("What is the minimum denominator: ")
  39.     b = input("What is the maximum denominator: ")
  40.  
  41.     # swapping the values if they're entered in the wrong order
  42.     if int(a) > int(b):
  43.         temp = a
  44.         a = b
  45.         b = temp
  46.    
  47.     print(f"a: {a}\nb: {b}")
  48.  
  49.     # checking to make sure the numbers are indeed numbers
  50.     if a.isalpha() or b.isalpha():
  51.         print("The input you provided is incorrect, make sure to enter only natural numbers.")
  52.         game()
  53.         return
  54.    
  55.     # once numbers have been confirmed as non-alphabetical, we turn them into natural numbers
  56.     a = abs(int(a))
  57.     b = abs(int(b))
  58.  
  59.     # points gamify the process
  60.     points = 0
  61.  
  62.     # correct answers increase the multiplier
  63.     correct = 0
  64.  
  65.     # multipier value of points
  66.     correct_multiplier = calculate_starting_multiplier(b)
  67.  
  68.     # chance left until games in
  69.     chances = get_difficulty()
  70.     starting_chances = chances
  71.  
  72.     while chances > 0:
  73.         # creating this rounds number
  74.         denominator = randrange(a, b + 1)
  75.  
  76.         if denominator == 1:
  77.             # if denominator is 1 we generate a random number between 1 and the max value as it'll be a natural number
  78.             numerator = randrange(1, b)
  79.         else:
  80.             numerator = randrange(1, denominator)
  81.        
  82.         correct_answer = str(numerator) if denominator == 1 else str(round(numerator / denominator, 3))
  83.         answer = input(f"{numerator}/{denominator} = ")
  84.  
  85.         # if correct add points
  86.         if answer == correct_answer:
  87.             # every 5 correct we update the multiplier
  88.             if correct % 5 == 0:
  89.                 correct_multiplier += 0.1
  90.  
  91.             correct += 1
  92.             points += (1 * correct_multiplier)
  93.             print("Good!")
  94.         else:
  95.             chances -= 1
  96.             print(f"{'x' * (starting_chances - chances)}")
  97.     print(f"Final Score: {round(points, 2)}")
  98.  
  99. game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement