Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import operator
- import mysql.connector
- try :
- cnx = mysql.connector.connect(user='root', password='1000 hbk',
- host='localhost',
- database='mydb')
- except Error as error:
- print(error)
- cursor = cnx.cursor()
- def get_int_input(prompt=''):
- while True:
- try:
- return int(input(prompt))
- except ValueError:
- print("Sorry,but we need a number")
- ID = input("what is your ID?")
- attempt = input("What attempt is this at the quiz? 1 for first, 2 for second or 3 for third?")
- if attempt == 3:
- attempt = attempt3
- OPERATIONS = [
- (operator.add, "+"),
- (operator.mul, "x"),
- (operator.sub, "-")
- ]
- NB_QUESTIONS = 10
- if __name__ == '__main__':
- name = input("What is your name?").title()
- print(name, ", Welcome to the Maths Test")
- score = 0
- for _ in range(NB_QUESTIONS):
- num1 = random.randint(1,10)
- num2 = random.randint(1,10)
- op, symbol = random.choice(OPERATIONS)
- print("What is", num1, symbol, num2)
- if get_int_input() == op(num1, num2):
- print("Correct")
- score += 1
- else:
- print("Incorrect")
- print("Well done", name, "you scored", score, "/", NB_QUESTIONS)
- print ("Thank you for doing this mathamatical quiz , goodbye ")
- if attempt == "1":
- add_record = ("INSERT INTO Class1"
- "(ID, Name, Score1) "
- "VALUES (%s, %s, %s)")
- data_record = (ID, name, score)
- cursor.execute(add_record, data_record)
- cnx.commit()
- if attempt == "2":
- cursor.execute("SELECT * FROM Class1 WHERE ID = %s", [ID])
- row = cursor.fetchone()
- cursor.execute("UPDATE Class1 SET Score2 = '%c' WHERE ID= '%c'")
- if attempt == "3":
- cursor.execute("SELECT * FROM Class1 WHERE ID = %s", [ID])
- row = cursor.fetchone()
- cursor.execute("UPDATE Class1 SET Score3 = '%c' WHERE ID= '%c'")
- cursor.close()
- cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement