Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.44 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.         answers = []
  157.         correctArea = (base*height)
  158.  
  159.         for counter in range(3):
  160.             answers.append(random.randint(1, 98))
  161.         shuffle(answers)
  162.         answers.append(correctArea)
  163.  
  164.         print("\n\nA rectangle a base of {0} and a height of {1}. Find the area of it.".format(base, height))
  165.         print("1) {0}".format(answers[0]))
  166.         print("2) {0}".format(answers[1]))
  167.         print("3) {0}".format(answers[2]))
  168.         print("4) {0}".format(answers[3]))
  169.         selection = int(input("Choice: "))
  170.  
  171.         if correctArea == selection:
  172.             print("Correct!")
  173.             score = score + 1
  174.             play = True
  175.         elif selection != correctArea:
  176.             print("Incorrect! The correct answer was {0}".format(correctArea))
  177.             quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
  178.             if quitOrContinue == 1:
  179.                 print("Your final score was {0}".format(score))
  180.                 time.sleep(1.5)
  181.                 stats(score, game)
  182.             else:
  183.                 play = True
  184.         else:
  185.             main()
  186.  
  187.        
  188.  
  189. def circle():
  190.     play = True
  191.     score = 0
  192.     game = "Circle"
  193.     pi = 3.1415926535897932384626433832795028841971693993751058209749445923
  194.    
  195.     while play == True: #Runs the game while the user doesn't quit
  196.         answers = []
  197.         radius = random.randint(1, 14)
  198.        
  199.         correctArea = (radius*radius)*pi
  200.         incorrectArea1 = random.randint(1, 98)
  201.         incorrectArea2 = random.randint(1, 98)
  202.         incorrectArea3 = random.randint(1, 98)
  203.        
  204.         answers.append(correctArea)
  205.         answers.append(incorrectArea1)
  206.         answers.append(incorrectArea2)
  207.         answers.append(incorrectArea3)
  208.        
  209.         random.shuffle(answers)
  210.  
  211.         for counter in range(answers):
  212.             print(counter)
  213.            
  214.  
  215.         selection = int(input("""
  216. A circle has a radius of {0}. Find the area of it.
  217. 1) {1}
  218. 2) {2}
  219. 3) {3}
  220. 4) {4}
  221. Choice: """.format(radius, incorrectArea1, correctArea, incorrectArea2, incorrectArea3)))
  222.  
  223.         if selection == correctArea:
  224.             print("Correct!")
  225.             score = score + 1
  226.             play = True
  227.         else:
  228.            
  229.             print("Incorrect!")
  230.             print("The correct answer was {0}".format(correctArea))
  231.             quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
  232.             if quitOrContinue == 1:
  233.                 print("Your final score was {0}".format(score))
  234.                 time.sleep(1.5)
  235.                 stats(score, game)
  236.             else:
  237.                 play = True
  238.  
  239.  
  240. def stats(score, game):
  241.     """ This function saves the users
  242.    score from their last game. It works
  243.    by taking the argument and placing it
  244.    in a file."""
  245.    
  246.     localtime = time.asctime(time.localtime(time.time())) #Saves the date in a variable
  247.     with open("stats.txt", mode="a", encoding="utf-8") as my_file:
  248.         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
  249.     time.sleep(1.5)
  250.     main()
  251.  
  252.  
  253. def viewStats():
  254.     """ This function allows the
  255.    user to view their stats. It works
  256.    by reading the stats.txt file and
  257.    printing its contents """
  258.  
  259.     with open("stats.txt", mode="r", encoding="utf-8") as my_file:
  260.         stats = my_file.read().splitlines()
  261.         print("\n")
  262.         for counter in stats:
  263.             print("•", counter)
  264.         main()
  265.  
  266.    
  267.        
  268.    
  269.  
  270. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement