Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- num_of_examples = 10
- max_n = 99
- min_n = 1
- def getPairFor(fun, first = None):
- changeFirst = True if not first else False
- while True:
- first = random.randint(min_n, max_n) if changeFirst else first
- second = random.randint(min_n, max_n)
- if 0 < fun(first, second) <= 99:
- return first, second, fun(first, second)
- def chooseRandomFunction():
- if random.randint(0,1) == 0:
- return lambda x, y: x + y, '+'
- else:
- return lambda x, y: x - y, '-'
- def twoNumbers():
- correct = 0
- for i in range(num_of_examples):
- fun, operation = chooseRandomFunction()
- first, second, result = getPairFor(fun)
- while True:
- print(str(first) + operation + str(second) + '=', end='')
- in_number = input()
- if in_number.isnumeric():
- res = int(in_number)
- if res == result:
- print('Bravooooooo!!!')
- correct += 1
- else:
- print('Pogrešno! Točan rezultate je: ' + str(result))
- break
- else:
- print('Moraš upisati broj!')
- print('Točnih ' + str(correct) + ' od ' + str(num_of_examples))
- def threeNumbers():
- correct = 0
- for i in range(num_of_examples):
- fun1, operation1 = chooseRandomFunction()
- fun2, operation2 = chooseRandomFunction()
- first1, second1, result1 = getPairFor(fun1)
- _, second2, result = getPairFor(fun2, first=result1)
- while True:
- string = str(first1) + operation1 + str(second1) + operation2 + str(second2) + '='
- print(string, end='')
- in_number = input()
- if in_number.isnumeric():
- res = int(in_number)
- if res == result:
- print('Bravooooooo!!!')
- correct += 1
- else:
- print('Pogrešno! Točan rezultate je: ' + str(result))
- break
- else:
- print('Moraš upisati broj!')
- print('TOČNIH ' + str(correct) + ' OD ' + str(num_of_examples))
- def main():
- while True:
- print('Izaberi vježbu:\n1. Dva broja\n2. Tri broja')
- option = input()
- if option == '1':
- twoNumbers()
- break
- elif option == '2':
- threeNumbers()
- break
- else:
- print('Krivi broj!')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement