Advertisement
Guest User

Weird A-Level Code Stuff

a guest
Sep 11th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.63 KB | None | 0 0
  1. import random
  2. from os.path import exists
  3. from time import sleep
  4.  
  5. def points(username, points):
  6.     with open(username+"points", 'a') as k:
  7.         k.write(points)
  8.  
  9. def rgen():
  10.     return random.randrange(3, 10)
  11. def others(area): # function rid
  12.     print("others")
  13.     if random.randrange(0,1):
  14.         print("others 1")
  15.         o= area+random.randrange(1, (area/10)+1)
  16.     else:
  17.         print("others 2")
  18.         o= area-random.randrange(1, (area/10)+1)
  19.     return o
  20.  
  21.  
  22. def tra(length, height):
  23.     return (length*height)/2
  24.  
  25. def rea(length, height):
  26.     return length*height
  27.  
  28. def game():
  29.     while True:
  30.         uf= input('Enter shape (rectangle/triangle) or menu to return to menu: ').lower()
  31.         if uf == "menu":
  32.             break
  33.         elif uf == 'triangle':
  34.             rh= rgen(); rl= rgen()
  35.             ra= float(tra(rh, rl))
  36.  
  37.         elif uf == 'rectangle':
  38.             rh= rgen()
  39.             rl= rgen()
  40.             while rl==rh:
  41.                 rl= rgen()
  42.             ra= float(rea(rl, rh))
  43.         else:
  44.             print("Enter one of the options")
  45.             continue
  46.  
  47.         l=[] # working
  48.         #print(l) # working
  49.         l.append(float(ra))
  50.         #print(l) # working
  51.         while len(l) <= 3:
  52.             #sleep(0.9)
  53.             #print("while len l")
  54.             #print(bool(len(l) < 5))
  55.  
  56.             if random.randrange(0, 2):
  57.                 #print("others 1")
  58.                 #try:
  59.                 v = ra + random.randrange(1, ((ra / 5)//1) + 3)
  60.                 #except Exception as e:
  61.                 #print(e)
  62.                 #print("true", v)
  63.             else:
  64.                 #print("others 2")
  65.                 #try:
  66.                 v = ra - random.randrange(1, ((ra / 5)//1) + 3)
  67.                 #except Exception as e:
  68.                 #print(e)
  69.                 #print(v, "oth2")
  70.             if v not in l:
  71.                 #sleep(1)
  72.                 #print(l, "tofore")
  73.                 l.append(float(v))
  74.                 #print(l, "appended v")
  75.  
  76.         """
  77.        while len(l) < 5:
  78.            print("while len(l) <= 4")
  79.            v= others(ra)
  80.            print(l)
  81.            if v not in l:
  82.                print("if v not in l")
  83.                l.append(v)
  84.        print(v)"""
  85.  
  86.         random.shuffle(l)
  87.         print("Random shuffled")
  88.         print('''Length: %i
  89.        Height: %i
  90.        Which of the following is the area: %a''' % (rl, rh, l))
  91.         while True:
  92.             try:
  93.                 ug= float(input())
  94.                 if ug not in l:
  95.                     print('Enter one of the options')
  96.                     continue
  97.                 if ug == ra:
  98.                     points(ue, '2\n')
  99.                     break
  100.                 else:
  101.                     while True:
  102.                         try:
  103.                             uh = float(input("Try again: "))
  104.                             if uh not in l:
  105.                                 print("Enter one of the options")
  106.                                 continue
  107.                             if uh == ug:
  108.                                 print("Enter a different option")
  109.                                 continue
  110.                             if uh == ra:
  111.                                 points(ue, '1\n')
  112.                                 break
  113.                             else:
  114.                                 print("Wrong, Answer was %i" % ra)
  115.  
  116.                         except ValueError:
  117.                             print("Enter one of the options")
  118.                             continue
  119.                         break
  120.             except ValueError:
  121.                 print ("Enter one of the options")
  122.                 continue
  123.             break
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. def wpoints(username):
  133.     k=open(username+"points", "w")
  134.     k.close()
  135.  
  136. def pwc(password, filename): #password correct
  137.     if open(filename).read() == password:
  138.         return True
  139.     else: return False
  140.  
  141. def wusername(username):
  142.     # create file with pw+username
  143.     fn= 'pw'+username
  144.     k=open(fn, 'w')
  145.     k.close()
  146.     return fn # return file name of user
  147.  
  148. def wpassword(password, filename):
  149.     # add in wusername created file username's password
  150.     with open(filename, 'w+') as k:
  151.         k.write(password)
  152.         k.close()
  153. def signin():
  154.     global ue
  155.     while True:
  156.         ue= input('Enter username: ')
  157.         if exists('pw'+ue):
  158.             break
  159.         else:
  160.             print('Enter correct username')
  161.             continue
  162.  
  163.     while True:
  164.         uf= input("Enter password for user %s" % ue)
  165.         if pwc(uf, 'pw'+ue):
  166.             break
  167.         else:
  168.             print("Incorrect password. Try again.")
  169.             continue
  170.  
  171.     game()
  172.  
  173.  
  174.  
  175.  
  176.  
  177. def cacc():
  178.     uc= input("Enter username: ")
  179.     while True:
  180.         ud= input('Enter password: ')
  181.         if len(ud) < 8 or len(ud) > 18:
  182.             print("Password must be between 8 and 18 characters")
  183.             continue
  184.         if not any(d.isdigit() for d in ud) or not any(d.isalpha() for d in ud):
  185.             print("Must contain letters and numbers")
  186.             continue
  187.         if not any(d.isupper() for d in ud) or not any(d.islower() for d in ud):
  188.             print("Must contain upper and lower case")
  189.             continue
  190.         break
  191.     try:
  192.         fun= wusername(uc)
  193.         wpassword(ud, fun)
  194.         wpoints(uc)
  195.     except IOError:
  196.         print('System cannot write to files; application will now close')
  197.         quit()
  198.  
  199.  
  200.  
  201. def choice():
  202.     while True:
  203.         ub= input("Do you have an account? (yes/no)").lower()
  204.         if ub == 'yes':
  205.             signin()
  206.         elif ub == 'no':
  207.             cacc()
  208.         else:
  209.             print("Enter either yes or no")
  210.             continue
  211.         break
  212.  
  213.  
  214. def vscore():
  215.     while True:
  216.         ui= input("View score for which user/return to menu: ")
  217.         if ui.lower() == "menu": break
  218.         if not exists(ui+"points"):
  219.             print("Account does not exist/ points file does not exist for user")
  220.         else:
  221.             k= open(ui+"points").read()
  222.             d=k.split('\n')
  223.             print("Points: %i" % sum(list(map(int, d))))
  224.             break
  225. while True:
  226.     while True:
  227.         try:
  228.             ua= int(input('''Do you wish to:
  229.            1. Sign in
  230.            2. View score
  231.            3. Quit'''))
  232.             if ua == 1:
  233.                 choice()
  234.             elif ua == 2:
  235.                 vscore()
  236.             elif ua == 3:
  237.                 break
  238.             else:
  239.                 print("Enter a valid value")
  240.                 continue
  241.         except ValueError:
  242.             print("Enter one of the options")
  243.             continue
  244.     break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement