Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import random
  2. num_of_examples = 10
  3. max_n = 99
  4. min_n = 1
  5.  
  6. def getPairFor(fun, first = None):
  7. changeFirst = True if not first else False
  8.  
  9. while True:
  10. first = random.randint(min_n, max_n) if changeFirst else first
  11. second = random.randint(min_n, max_n)
  12.  
  13. if 0 < fun(first, second) <= 99:
  14. return first, second, fun(first, second)
  15.  
  16. def chooseRandomFunction():
  17. if random.randint(0,1) == 0:
  18. return lambda x, y: x + y, '+'
  19. else:
  20. return lambda x, y: x - y, '-'
  21.  
  22.  
  23. def twoNumbers():
  24. correct = 0
  25.  
  26. for i in range(num_of_examples):
  27. fun, operation = chooseRandomFunction()
  28. first, second, result = getPairFor(fun)
  29.  
  30. while True:
  31. print(str(first) + operation + str(second) + '=', end='')
  32. in_number = input()
  33.  
  34. if in_number.isnumeric():
  35. res = int(in_number)
  36.  
  37. if res == result:
  38. print('Bravooooooo!!!')
  39. correct += 1
  40. else:
  41. print('Pogrešno! Točan rezultate je: ' + str(result))
  42.  
  43. break
  44. else:
  45. print('Moraš upisati broj!')
  46.  
  47. print('Točnih ' + str(correct) + ' od ' + str(num_of_examples))
  48.  
  49.  
  50. def threeNumbers():
  51. correct = 0
  52.  
  53. for i in range(num_of_examples):
  54. fun1, operation1 = chooseRandomFunction()
  55. fun2, operation2 = chooseRandomFunction()
  56. first1, second1, result1 = getPairFor(fun1)
  57. _, second2, result = getPairFor(fun2, first=result1)
  58.  
  59. while True:
  60. string = str(first1) + operation1 + str(second1) + operation2 + str(second2) + '='
  61. print(string, end='')
  62. in_number = input()
  63.  
  64. if in_number.isnumeric():
  65. res = int(in_number)
  66.  
  67. if res == result:
  68. print('Bravooooooo!!!')
  69. correct += 1
  70. else:
  71. print('Pogrešno! Točan rezultate je: ' + str(result))
  72.  
  73. break
  74. else:
  75. print('Moraš upisati broj!')
  76.  
  77. print('TOČNIH ' + str(correct) + ' OD ' + str(num_of_examples))
  78.  
  79.  
  80. def main():
  81. while True:
  82. print('Izaberi vježbu:\n1. Dva broja\n2. Tri broja')
  83. option = input()
  84.  
  85. if option == '1':
  86. twoNumbers()
  87. break
  88. elif option == '2':
  89. threeNumbers()
  90. break
  91. else:
  92. print('Krivi broj!')
  93.  
  94. if __name__ == '__main__':
  95. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement