Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import os
- import random
- from random import shuffle
- def start():
- try:
- loginOrRegister = int(input("""
- --- Welcome ---
- 1) Login
- 2) Register
- Please select an option: """))
- if loginOrRegister == 1:
- login()
- elif loginOrRegister == 2:
- register()
- else:
- start()
- except ValueError: #If the user doesn't enter a number, let them try again
- print("You must enter a number")
- time.sleep(1.5)
- start()
- def login():
- """ This function allows the user to
- login by comparing their username/password
- to a file that they created during the
- registration process"""
- 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
- userList = my_file.read().splitlines() #Saves the username/password as a list
- print("\n\n --- Login ---")
- nameInput = input("Enter the username: ")
- passInput = input("Enter the password: ")
- if userList[0] == nameInput and userList[1] == passInput: #Checks if the username/password is correct
- print("Your login is successful")
- main()
- else:
- print("Incorrect username/password")
- time.sleep(1.5)
- login()
- def register():
- """ This function allows the user to
- create an account. It works by asking
- the user for a username/password, saving
- the two values into a list and then saving
- that list into a .txt file. If a users
- password is less than 7 characters long
- then they must enter a new one. """
- usernamePassword = [] #Creates a list to save the username/password into (allows you to save to a file)
- print("\n\n --- Register ---")
- username = input("Please enter a username: ")
- password = input("Please enter a password: ")
- lengthOfPassword = len(password) #Checks the length of the password for security
- if lengthOfPassword < 7:
- print("Your password must be longer than 6 characters. Please try again.")
- time.sleep(1.5)
- register()
- else: #Only allows successful registration if password is more than 6 characters
- usernamePassword.append(username) #Saves username into a list
- usernamePassword.append(password) #Saves password into a list
- with open("usernamePassword.txt", mode="w", encoding="utf-8") as my_file:
- for counter in usernamePassword:
- my_file.write(counter+"\n") #Saves list as a file
- print("You have successfully registered")
- time.sleep(1.5)
- login()
- def main():
- """ This function allows
- the user to choose the shape
- they would like to practice
- on """
- try:
- shapeChoice = int(input("""\n\n
- --- Area Trainer ---
- 1) Triangle
- 2) Rectangle
- 3) Circle
- 4) View statistics
- Enter the number of the shape you would like to practice on: """))
- if shapeChoice == 1:
- triangle()
- elif shapeChoice == 2:
- rectangle()
- elif shapeChoice == 3:
- circle()
- elif shapeChoice == 4:
- viewStats()
- else:
- main()
- except ValueError: #If the user doesn't enter a number, let them try again
- print("You must enter a number")
- time.sleep(1.5)
- main()
- def triangle():
- play = True
- score = 0
- game = "Triangle"
- while play == True: #Runs the game while the user doesn't quit
- base = random.randint(1, 14)
- height = random.randint(1, 14)
- correctArea = (base*height)/2
- incorrectArea1 = random.randint(1, 98)
- incorrectArea2 = random.randint(1, 98)
- incorrectArea3 = random.randint(1, 98)
- selection = int(input("""
- A triangle has a base of {0} and a height of {1}. Find the area of it.
- 1) {2}
- 2) {3}
- 3) {4}
- 4) {5}
- Choice: """.format(base, height, incorrectArea1, correctArea, incorrectArea2, incorrectArea3)))
- if selection == 2:
- print("Correct!")
- score = score + 1
- play = True
- else:
- print("Incorrect!")
- print("The correct answer was {0}".format(correctArea))
- quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
- if quitOrContinue == 1:
- print("Your final score was {0}".format(score))
- time.sleep(1.5)
- stats(score, game)
- else:
- play = True
- def rectangle():
- play = True
- score = 0
- game = "Rectangle"
- while play == True: #Runs the game while the user doesn't quit
- base = random.randint(1, 14)
- height = random.randint(1, 14)
- correctArea = (base*height)
- incorrectArea1 = random.randint(1, 98)
- incorrectArea2 = random.randint(1, 98)
- incorrectArea3 = random.randint(1, 98)
- answers = []
- correctArea = (base*height)
- incorrectArea1 = random.randint(1, 98)
- incorrectArea2 = random.randint(1, 98)
- incorrectArea3 = random.randint(1, 98)
- answers.append(incorrectArea2)
- answers.append(incorrectArea1)
- answers.append(correctArea)
- answers.append(incorrectArea3)
- random.shuffle(answers)
- print("A rectangle a base of {0} and a height of {1}. Find the area of it.".format(base, height))
- print("1) {0}".format(answers[0]))
- print("2) {0}".format(answers[1]))
- print("3) {0}".format(answers[2]))
- print("4) {0}".format(answers[3]))
- selection = int(input("Choice: "))
- if selection == correctArea:
- print("Correct!")
- score = score + 1
- play = True
- else:
- print("Incorrect!")
- print("The correct answer was {0}".format(correctArea))
- quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
- if quitOrContinue == 1:
- print("Your final score was {0}".format(score))
- time.sleep(1.5)
- stats(score, game)
- else:
- play = True
- def circle():
- play = True
- score = 0
- game = "Circle"
- pi = 3.1415926535897932384626433832795028841971693993751058209749445923
- while play == True: #Runs the game while the user doesn't quit
- answers = []
- radius = random.randint(1, 14)
- correctArea = (radius*radius)*pi
- incorrectArea1 = random.randint(1, 98)
- incorrectArea2 = random.randint(1, 98)
- incorrectArea3 = random.randint(1, 98)
- answers.append(correctArea)
- answers.append(incorrectArea1)
- answers.append(incorrectArea2)
- answers.append(incorrectArea3)
- random.shuffle(answers)
- for counter in range(answers):
- print(counter)
- selection = int(input("""
- A circle has a radius of {0}. Find the area of it.
- 1) {1}
- 2) {2}
- 3) {3}
- 4) {4}
- Choice: """.format(radius, incorrectArea1, correctArea, incorrectArea2, incorrectArea3)))
- if selection == correctArea:
- print("Correct!")
- score = score + 1
- play = True
- else:
- print("Incorrect!")
- print("The correct answer was {0}".format(correctArea))
- quitOrContinue = int(input("\nWould you like to quit (1) or continue (2)?: "))
- if quitOrContinue == 1:
- print("Your final score was {0}".format(score))
- time.sleep(1.5)
- stats(score, game)
- else:
- play = True
- def stats(score, game):
- """ This function saves the users
- score from their last game. It works
- by taking the argument and placing it
- in a file."""
- localtime = time.asctime(time.localtime(time.time())) #Saves the date in a variable
- with open("stats.txt", mode="a", encoding="utf-8") as my_file:
- 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
- time.sleep(1.5)
- main()
- def viewStats():
- """ This function allows the
- user to view their stats. It works
- by reading the stats.txt file and
- printing its contents """
- with open("stats.txt", mode="r", encoding="utf-8") as my_file:
- stats = my_file.read().splitlines()
- print("\n")
- for counter in stats:
- print("•", counter)
- main()
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement