Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import random
  2. import operator
  3. import mysql.connector
  4. try :
  5. cnx = mysql.connector.connect(user='root', password='1000 hbk',
  6. host='localhost',
  7. database='mydb')
  8.  
  9. except Error as error:
  10. print(error)
  11.  
  12. cursor = cnx.cursor()
  13. def get_int_input(prompt=''):
  14. while True:
  15. try:
  16. return int(input(prompt))
  17. except ValueError:
  18. print("Sorry,but we need a number")
  19.  
  20.  
  21. ID = input("what is your ID?")
  22.  
  23. attempt = input("What attempt is this at the quiz? 1 for first, 2 for second or 3 for third?")
  24.  
  25.  
  26.  
  27. if attempt == 3:
  28. attempt = attempt3
  29.  
  30.  
  31. OPERATIONS = [
  32. (operator.add, "+"),
  33. (operator.mul, "x"),
  34. (operator.sub, "-")
  35. ]
  36.  
  37. NB_QUESTIONS = 10
  38.  
  39.  
  40.  
  41. if __name__ == '__main__':
  42. name = input("What is your name?").title()
  43. print(name, ", Welcome to the Maths Test")
  44.  
  45. score = 0
  46. for _ in range(NB_QUESTIONS):
  47. num1 = random.randint(1,10)
  48. num2 = random.randint(1,10)
  49. op, symbol = random.choice(OPERATIONS)
  50. print("What is", num1, symbol, num2)
  51. if get_int_input() == op(num1, num2):
  52. print("Correct")
  53. score += 1
  54. else:
  55. print("Incorrect")
  56.  
  57. print("Well done", name, "you scored", score, "/", NB_QUESTIONS)
  58.  
  59. print ("Thank you for doing this mathamatical quiz , goodbye ")
  60.  
  61. if attempt == "1":
  62. add_record = ("INSERT INTO Class1"
  63. "(ID, Name, Score1) "
  64. "VALUES (%s, %s, %s)")
  65.  
  66. data_record = (ID, name, score)
  67.  
  68. cursor.execute(add_record, data_record)
  69. cnx.commit()
  70.  
  71. if attempt == "2":
  72. cursor.execute("SELECT * FROM Class1 WHERE ID = %s", [ID])
  73. row = cursor.fetchone()
  74. cursor.execute("UPDATE Class1 SET Score2 = '%c' WHERE ID= '%c'")
  75.  
  76. if attempt == "3":
  77. cursor.execute("SELECT * FROM Class1 WHERE ID = %s", [ID])
  78. row = cursor.fetchone()
  79. cursor.execute("UPDATE Class1 SET Score3 = '%c' WHERE ID= '%c'")
  80.  
  81.  
  82. cursor.close()
  83. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement