Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. from random import *
  2.  
  3. import json
  4.  
  5.  
  6.  
  7. status = False
  8.  
  9. mtopic = 0
  10.  
  11.  
  12.  
  13. def Signup():
  14.  
  15.  
  16.  
  17. print("you have chosen to register")
  18.  
  19.  
  20.  
  21. name = input("What is your name?")
  22.  
  23.  
  24.  
  25. age = input("How old are you?")
  26.  
  27.  
  28.  
  29. username = age + name[:3]
  30.  
  31. filename = username + ".txt"
  32.  
  33.  
  34.  
  35. print("Your username is " + username)
  36.  
  37.  
  38.  
  39. password = input("please enter your password?")
  40.  
  41.  
  42.  
  43. file = {}
  44.  
  45.  
  46.  
  47. file['a'] = {
  48.  
  49. 'name': name,
  50.  
  51. 'age': age,
  52.  
  53. 'password': password,
  54.  
  55. 'username': username
  56.  
  57. }
  58.  
  59.  
  60.  
  61. s = json.dumps(file)
  62.  
  63. print(s)
  64.  
  65.  
  66. with open(username + ".txt", "w") as c:
  67.  
  68. c.write(s)
  69.  
  70.  
  71.  
  72.  
  73.  
  74. def Login():
  75.  
  76. global status
  77.  
  78. user = input("What is your username")
  79.  
  80. file = open(user + ".txt", "r")
  81.  
  82. data = json.load(file);
  83.  
  84. password = input("What is your password")
  85.  
  86. if password == data['a']['password']:
  87.  
  88. print ("LOGIN SUCESS")
  89.  
  90. status = True
  91.  
  92. return status
  93.  
  94. else:
  95.  
  96. print ("WRONG \n Type Login() to retry")
  97.  
  98.  
  99.  
  100.  
  101.  
  102. status = 0
  103.  
  104. i = int(input("1. register or 2. login?"))
  105.  
  106. if i == 1:
  107.  
  108. Signup()
  109.  
  110. elif i == 2:
  111.  
  112. status = Login()
  113.  
  114. else:
  115.  
  116. print("RESTART YOU CANT FOLLW INSTURCTIONS")
  117.  
  118.  
  119. def addToScoreboard(nameToAdd, scoreToAdd):
  120.  
  121.  
  122. file = open("Scoreboard.csv", "a")
  123.  
  124. file.write(str(nameToAdd) + "," + str(scoreToAdd))
  125.  
  126. file.close
  127.  
  128. def menu():
  129. #print ("Call Menu")
  130. userChoice = 0
  131.  
  132. print ("Welcome to the Quiz")
  133. print("\n \t Enter Choice 1 to start the quiz")
  134. print ("\t Enter Choice 2 to display Leaderboard")
  135. print("\t Enter 3 to quit \n")
  136.  
  137. userChoice = int(input("Please Enter your Choice"))
  138.  
  139. while userChoice < 1 or userChoice > 3:
  140.  
  141. userChoice = int(input("Invalid: Please Enter your Choice 1 to 3 only"))
  142.  
  143. return userChoice
  144.  
  145. def startQuiz():
  146. #print ("Start Quiz")
  147. userScore = 0
  148.  
  149. UserName = ""
  150.  
  151. file = open("quiz.csv", "r")
  152.  
  153. rawData = file.readlines()
  154.  
  155. file.close()
  156.  
  157. questions = []
  158. answer1 = []
  159. answer2 = []
  160. answer3 = []
  161. answer4 = []
  162. correctAnswer = []
  163.  
  164. for index in range(len(rawData)):
  165.  
  166. splitData = rawData[index].split(",")
  167.  
  168. questions.append(splitData[0].strip())
  169. answer1.append(splitData[1].strip())
  170. answer2.append(splitData[2].strip())
  171. answer3.append(splitData[3].strip())
  172. answer4.append(splitData[4].strip())
  173. correctAnswer.append(splitData[5].strip())
  174.  
  175. for index in range(0,9):
  176.  
  177. randomQ = randint(0, len(questions)- 1)
  178. #ISSUE HERE
  179.  
  180. print (questions[randomQ])
  181.  
  182. print("\t1",answer1[randomQ])
  183. print("\t2",answer2[randomQ])
  184. print("\t3",answer3[randomQ])
  185. print("\t4",answer4[randomQ])
  186.  
  187. print("")
  188.  
  189. userAnswer = int(input("Enter choice 1 to 4 for your answer"))
  190.  
  191. while userAnswer < 1 or userAnswer > 4:
  192. userAnswer = int(input("INVALID CHOICE: Enter Choice 1 to 4 for your answer"))
  193.  
  194.  
  195. userAnswer = userAnswer - 1
  196.  
  197. if userAnswer == int(correctAnswer[randomQ]):
  198.  
  199. userScore = userScore + 1
  200.  
  201. print("Correct")
  202.  
  203. else:
  204. print("Incorrect")
  205.  
  206.  
  207. print("Congratulations, you scored ", userScore)
  208.  
  209. userName = input("Please enter your name for the leaderboard")
  210.  
  211. addToScoreboard(userName, userScore)
  212.  
  213.  
  214.  
  215.  
  216. def displayLeaderboard():
  217. #print ("Display Leaderboard")
  218. usernames = []
  219. userPoints = []
  220.  
  221. file = open("Scoreboard.csv", "r")
  222.  
  223. rawData = file.readlines()
  224.  
  225. file.close()
  226.  
  227. for index in range(len(rawData)):
  228. splitData = rawData[index].split(",")
  229. usernames.append(splitData[0].strip())
  230. userPoints.append(splitData[1].strip())
  231.  
  232. #bubblesort to sort data
  233. for outer in range(0, len(userPoints)-1):
  234. for inner in range(0, len(userPoints)-1):
  235. if int(userPoints[inner]) < int(userPoints[inner+1]):
  236.  
  237. temp = userPoints[inner]
  238. userPoints[inner] = userPoints[inner + 1]
  239. userPoints [inner + 1] = temp
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. print("{0:<10s}\t{1:<15s}\t".format ("User Name ", "Score"))
  248.  
  249. for index in range(0, len(usernames)):
  250. print("{0:<10s}\t{1:<15s}\t".format (usernames[index],userPoints[index]))
  251.  
  252.  
  253.  
  254. #Main Program
  255.  
  256. userChoice = 0
  257.  
  258. while userChoice != 3:
  259. userChoice = menu()
  260.  
  261. if userChoice == 1:
  262.  
  263.  
  264. startQuiz()
  265.  
  266. elif userChoice == 2:
  267. displayLeaderboard()
  268.  
  269. else:
  270. print ("Thank you for using the program")
  271. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement