Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import *
- import json
- status = False
- mtopic = 0
- def Signup():
- print("you have chosen to register")
- name = input("What is your name?")
- age = input("How old are you?")
- username = age + name[:3]
- filename = username + ".txt"
- print("Your username is " + username)
- password = input("please enter your password?")
- file = {}
- file['a'] = {
- 'name': name,
- 'age': age,
- 'password': password,
- 'username': username
- }
- s = json.dumps(file)
- print(s)
- with open(username + ".txt", "w") as c:
- c.write(s)
- def Login():
- global status
- user = input("What is your username")
- file = open(user + ".txt", "r")
- data = json.load(file);
- password = input("What is your password")
- if password == data['a']['password']:
- print ("LOGIN SUCESS")
- status = True
- return status
- else:
- print ("WRONG \n Type Login() to retry")
- status = 0
- i = int(input("1. register or 2. login?"))
- if i == 1:
- Signup()
- elif i == 2:
- status = Login()
- else:
- print("RESTART YOU CANT FOLLW INSTURCTIONS")
- def addToScoreboard(nameToAdd, scoreToAdd):
- file = open("Scoreboard.csv", "a")
- file.write(str(nameToAdd) + "," + str(scoreToAdd))
- file.close
- def menu():
- #print ("Call Menu")
- userChoice = 0
- print ("Welcome to the Quiz")
- print("\n \t Enter Choice 1 to start the quiz")
- print ("\t Enter Choice 2 to display Leaderboard")
- print("\t Enter 3 to quit \n")
- userChoice = int(input("Please Enter your Choice"))
- while userChoice < 1 or userChoice > 3:
- userChoice = int(input("Invalid: Please Enter your Choice 1 to 3 only"))
- return userChoice
- def startQuiz():
- #print ("Start Quiz")
- userScore = 0
- UserName = ""
- file = open("quiz.csv", "r")
- rawData = file.readlines()
- file.close()
- questions = []
- answer1 = []
- answer2 = []
- answer3 = []
- answer4 = []
- correctAnswer = []
- for index in range(len(rawData)):
- splitData = rawData[index].split(",")
- questions.append(splitData[0].strip())
- answer1.append(splitData[1].strip())
- answer2.append(splitData[2].strip())
- answer3.append(splitData[3].strip())
- answer4.append(splitData[4].strip())
- correctAnswer.append(splitData[5].strip())
- for index in range(0,9):
- randomQ = randint(0, len(questions)- 1)
- #ISSUE HERE
- print (questions[randomQ])
- print("\t1",answer1[randomQ])
- print("\t2",answer2[randomQ])
- print("\t3",answer3[randomQ])
- print("\t4",answer4[randomQ])
- print("")
- userAnswer = int(input("Enter choice 1 to 4 for your answer"))
- while userAnswer < 1 or userAnswer > 4:
- userAnswer = int(input("INVALID CHOICE: Enter Choice 1 to 4 for your answer"))
- userAnswer = userAnswer - 1
- if userAnswer == int(correctAnswer[randomQ]):
- userScore = userScore + 1
- print("Correct")
- else:
- print("Incorrect")
- print("Congratulations, you scored ", userScore)
- userName = input("Please enter your name for the leaderboard")
- addToScoreboard(userName, userScore)
- def displayLeaderboard():
- #print ("Display Leaderboard")
- usernames = []
- userPoints = []
- file = open("Scoreboard.csv", "r")
- rawData = file.readlines()
- file.close()
- for index in range(len(rawData)):
- splitData = rawData[index].split(",")
- usernames.append(splitData[0].strip())
- userPoints.append(splitData[1].strip())
- #bubblesort to sort data
- for outer in range(0, len(userPoints)-1):
- for inner in range(0, len(userPoints)-1):
- if int(userPoints[inner]) < int(userPoints[inner+1]):
- temp = userPoints[inner]
- userPoints[inner] = userPoints[inner + 1]
- userPoints [inner + 1] = temp
- print("{0:<10s}\t{1:<15s}\t".format ("User Name ", "Score"))
- for index in range(0, len(usernames)):
- print("{0:<10s}\t{1:<15s}\t".format (usernames[index],userPoints[index]))
- #Main Program
- userChoice = 0
- while userChoice != 3:
- userChoice = menu()
- if userChoice == 1:
- startQuiz()
- elif userChoice == 2:
- displayLeaderboard()
- else:
- print ("Thank you for using the program")
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement