Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.80 KB | None | 0 0
  1. import time
  2. import os
  3. import random
  4. from random import shuffle
  5.  
  6.  
  7. def start():
  8.     try:
  9.         loginOrRegister = int(input("""
  10.    --- Welcome ---
  11. 1) Login
  12. 2) Register
  13. Please select an option: """))
  14.  
  15.         if loginOrRegister == 1:
  16.             login()
  17.         elif loginOrRegister == 2:
  18.             register()
  19.         else:
  20.             start()
  21.     except ValueError: #If the user doesn't enter a number, let them try again
  22.         print("You must enter a number")
  23.         time.sleep(1.5)
  24.         start()
  25.  
  26.  
  27. def login():
  28.     """ This function allows the user to
  29.    login by comparing their username/password
  30.    to a file that they created during the
  31.    registration process"""
  32.    
  33.     with open("usernamePassword.txt", mode="r", encoding="utf-8") as my_file: #Opens the text file, saves everything in it under the 'my_file' variable
  34.         userList = my_file.read().splitlines() #Saves the username/password as a list
  35.  
  36.         print("\n\n    --- Login ---")
  37.         nameInput = input("Enter the username: ")
  38.         passInput = input("Enter the password: ")
  39.    
  40.         if userList[0] == nameInput and userList[1] == passInput: #Checks if the username/password is correct
  41.             print("Your login is successful")
  42.             main()
  43.         else:
  44.             print("Incorrect username/password")
  45.             time.sleep(1.5)
  46.             login()
  47.  
  48.  
  49. def register():
  50.     """ This function allows the user to
  51.    create an account. It works by asking
  52.    the user for a username/password, saving
  53.    the two values into a list and then saving
  54.    that list into a .txt file. If a users
  55.    password is less than 7 characters long
  56.    then they must enter a new one. """
  57.    
  58.     usernamePassword = [] #Creates a list to save the username/password into (allows you to save to a file)
  59.     print("\n\n    --- Register ---")
  60.     username = input("Please enter a username: ")
  61.     password = input("Please enter a password: ")
  62.  
  63.     lengthOfPassword = len(password) #Checks the length of the password for security
  64.     if lengthOfPassword < 7:
  65.         print("Your password must be longer than 6 characters. Please try again.")
  66.         time.sleep(1.5)
  67.         register()
  68.     else: #Only allows successful registration if password is more than 6 characters
  69.         usernamePassword.append(username) #Saves username into a list
  70.         usernamePassword.append(password) #Saves password into a list
  71.        
  72.         with open("usernamePassword.txt", mode="w", encoding="utf-8") as my_file:
  73.             for counter in usernamePassword:
  74.                 my_file.write(counter+"\n") #Saves list as a file
  75.         print("You have successfully registered")
  76.         time.sleep(1.5)
  77.         login()
  78.  
  79.  
  80. def main():
  81.     """ This function allows
  82.    the user to choose the shape
  83.    they would like to practice
  84.    on """
  85.     try:
  86.         shapeChoice = int(input("""\n\n
  87.    --- Area Trainer ---
  88. 1) Triangle
  89. 2) Rectangle
  90. 3) Circle
  91. 4) View statistics
  92. Enter the number of the shape you would like to practice on: """))
  93.  
  94.         if shapeChoice == 1:
  95.             triangle()
  96.         elif shapeChoice == 2:
  97.             rectangle()
  98.         elif shapeChoice == 3:
  99.             circle()
  100.         elif shapeChoice == 4:
  101.             viewStats()
  102.         else:
  103.             main()
  104.     except ValueError: #If the user doesn't enter a number, let them try again
  105.         print("You must enter a number")
  106.         time.sleep(1.5)
  107.         main()
  108.  
  109.  
  110. def triangle():
  111.     play = True
  112.     score = 0
  113.     game = "Triangle"
  114.    
  115.     while play == True: #Runs the game while the user doesn't quit
  116.         base = random.randint(1, 14)
  117.         height = random.randint(1, 14)
  118.         correctArea = (base*height)/2
  119.         incorrectArea1 = random.randint(1, 98)
  120.         incorrectArea2 = random.randint(1, 98)
  121.         incorrectArea3 = random.randint(1, 98)
  122.  
  123.         selection = int(input("""
  124. A triangle has a base of {0} and a height of {1}. Find the area of it.
  125. 1) {2}
  126. 2) {3}
  127. 3) {4}
  128. 4) {5}
  129. Choice: """.format(base, height, incorrectArea1, correctArea, incorrectArea2, incorrectArea3)))
  130.  
  131.         if selection == 2:
  132.             print("Correct!")
  133.             score = score + 1
  134.             play = True
  135.         else:
  136.            
  137.             print("Incorrect!")
  138.             print("The correct answer was {0}".format(correctArea))
  139.             quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
  140.             if quitOrContinue == 1:
  141.                 print("Your final score was {0}".format(score))
  142.                 time.sleep(1.5)
  143.                 stats(score, game)
  144.             else:
  145.                 play = True
  146.  
  147.  
  148. def rectangle():
  149.     play = True
  150.     score = 0
  151.     game = "Rectangle"
  152.    
  153.     while play == True: #Runs the game while the user doesn't quit
  154.         base = random.randint(1, 14)
  155.         height = random.randint(1, 14)
  156.         correctArea = (base*height)
  157.         incorrectArea1 = random.randint(1, 98)
  158.         incorrectArea2 = random.randint(1, 98)
  159.         incorrectArea3 = random.randint(1, 98)
  160.  
  161.         answers = []
  162.        
  163.         correctArea = (base*height)
  164.         incorrectArea1 = random.randint(1, 98)
  165.         incorrectArea2 = random.randint(1, 98)
  166.         incorrectArea3 = random.randint(1, 98)
  167.        
  168.         answers.append(incorrectArea2)
  169.         answers.append(incorrectArea1)
  170.         answers.append(correctArea)
  171.         answers.append(incorrectArea3)
  172.        
  173.         random.shuffle(answers)
  174.  
  175.         print("A rectangle a base of {0} and a height of {1}. Find the area of it.".format(base, height))
  176.         print("1) {0}".format(answers[0]))
  177.         print("2) {0}".format(answers[1]))
  178.         print("3) {0}".format(answers[2]))
  179.         print("4) {0}".format(answers[3]))
  180.         selection = int(input("Choice: "))
  181.  
  182.  
  183.         if selection == correctArea:
  184.             print("Correct!")
  185.             score = score + 1
  186.             play = True
  187.         else:
  188.            
  189.             print("Incorrect!")
  190.             print("The correct answer was {0}".format(correctArea))
  191.             quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
  192.             if quitOrContinue == 1:
  193.                 print("Your final score was {0}".format(score))
  194.                 time.sleep(1.5)
  195.                 stats(score, game)
  196.             else:
  197.                 play = True
  198.  
  199.        
  200.  
  201. def circle():
  202.     play = True
  203.     score = 0
  204.     game = "Circle"
  205.     pi = 3.1415926535897932384626433832795028841971693993751058209749445923
  206.    
  207.     while play == True: #Runs the game while the user doesn't quit
  208.         answers = []
  209.         radius = random.randint(1, 14)
  210.        
  211.         correctArea = (radius*radius)*pi
  212.         incorrectArea1 = random.randint(1, 98)
  213.         incorrectArea2 = random.randint(1, 98)
  214.         incorrectArea3 = random.randint(1, 98)
  215.        
  216.         answers.append(correctArea)
  217.         answers.append(incorrectArea1)
  218.         answers.append(incorrectArea2)
  219.         answers.append(incorrectArea3)
  220.        
  221.         random.shuffle(answers)
  222.  
  223.         for counter in range(answers):
  224.             print(counter)
  225.            
  226.  
  227.         selection = int(input("""
  228. A circle has a radius of {0}. Find the area of it.
  229. 1) {1}
  230. 2) {2}
  231. 3) {3}
  232. 4) {4}
  233. Choice: """.format(radius, incorrectArea1, correctArea, incorrectArea2, incorrectArea3)))
  234.  
  235.         if selection == correctArea:
  236.             print("Correct!")
  237.             score = score + 1
  238.             play = True
  239.         else:
  240.            
  241.             print("Incorrect!")
  242.             print("The correct answer was {0}".format(correctArea))
  243.             quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
  244.             if quitOrContinue == 1:
  245.                 print("Your final score was {0}".format(score))
  246.                 time.sleep(1.5)
  247.                 stats(score, game)
  248.             else:
  249.                 play = True
  250.  
  251.  
  252. def stats(score, game):
  253.     """ This function saves the users
  254.    score from their last game. It works
  255.    by taking the argument and placing it
  256.    in a file."""
  257.    
  258.     localtime = time.asctime(time.localtime(time.time())) #Saves the date in a variable
  259.     with open("stats.txt", mode="a", encoding="utf-8") as my_file:
  260.         my_file.write("\nOn {0} you played {1} and scored {2}".format(localtime, game, score)) #Prints the date, game the user played and the score they got on a new line in the stats.txt file
  261.     time.sleep(1.5)
  262.     main()
  263.  
  264.  
  265. def viewStats():
  266.     """ This function allows the
  267.    user to view their stats. It works
  268.    by reading the stats.txt file and
  269.    printing its contents """
  270.  
  271.     with open("stats.txt", mode="r", encoding="utf-8") as my_file:
  272.         stats = my_file.read().splitlines()
  273.         print("\n")
  274.         for counter in stats:
  275.             print("•", counter)
  276.         main()
  277.  
  278.    
  279.        
  280.    
  281.  
  282. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement