Advertisement
Guest User

random math quiz

a guest
Sep 5th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. himport sys
  2. from random import randint as rd
  3.  
  4.  
  5. class mathquiz:
  6.     operations = ['+','-','x','/']
  7.  
  8.     #op: operation, se: start-end
  9.     def __init__(self,op,se):
  10.         self.op,self.se = op,se
  11.  
  12.     def create(self):
  13.         res = (
  14.                 rd(int(self.se[0]),int(self.se[1])),
  15.                 rd(int(self.se[0]),int(self.se[1]))
  16.                 )
  17.         return [res[0],self.op,res[1]]
  18.  
  19.     def losing(self,poin):
  20.         if poin <= 1:
  21.            return sys.exit('\n[#] Yeahhh, You lose, Trash!')
  22.  
  23.     def question(self):
  24.         if self.op not in self.operations:
  25.             sys.exit('operations failure')
  26.  
  27.         poin = 0
  28.         while True:
  29.             a,b,c = self.create()
  30.             b="*" if b and "x" else b;q = f"{a} {b} {c}"
  31.             answer = input('[?] '+q+' = ')
  32.             sys.exit('\n[#] Bye Bye!') if answer == 'q' else answer
  33.  
  34.             try:
  35.                 if answer == '':
  36.                     print('[!] Question Skipped, Poin -5')
  37.                     poin -= 5
  38.                     self.losing(poin)
  39.                     continue
  40.                 elif eval(q) == int(answer):
  41.                     poin += 10
  42.                     print (f'[-] Benar! Poin kamu: {poin}')
  43.                 else:
  44.                     print (f'[!] Salah, Jawaban: {eval(q)}. Poin -3')
  45.                     poin -= 3
  46.                     self.losing(poin)
  47.  
  48.             except ValueError:
  49.                 print('[!] Your input invalid')
  50.                 continue
  51.  
  52. if __name__=="__main__":
  53.     run = mathquiz(sys.argv[1],sys.argv[2].split('-'))
  54.     run.question()
  55.  
  56.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement