jeudi 31 décembre 2015

What's wrong with my sqlite?

When I run the code, the program runs with no errors but leaves a blank space after the asking the 'class number'. I can't tell what's wrong. I've not yet finished with the code so just want to see if it works as it's supposed to but haven't been able to get that far yet.

user_name = input('Enter a name: ')

total_score = 0
x=0
while x<10:
    import random

    signs = ['+', '-', '*']
    sign = random.choice(signs)
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)

    print(num1, sign, num2)

    answer = int(eval(str(num1) + sign + str(num2)))

    userAnswer= int(input("= "))

    if userAnswer != answer:
        print ("Incorrect. The right answer is {}.".format(answer))
    else:
        print('Correct')
        total_score = total_score +1 
    x=x+1

if total_score == 10:
    print('Wow',user_name,'!','All 10 of your answers were correct!')
else:
    print (total_score, 'out of 10 were correct!')

from datetime import datetime

now = datetime.now().strftime('%Y-%m-%d %H:%M')
class_number = int(input('Please enter your class number: '))

import sqlite3
if class_number in ['1','2','3']:

    conn = sqlite3.connect('class{}.db')
    c = conn.cursor()

    c.execute('''CREATE TABLE CLS1
             (Username, Score, Date)''')

    c.execute("INSERT INTO CLS1 VALUES (user_name, total_score, now)")

    conn.commit()

import sqlite3
if class_number in ['1','2','3']:

    conn = sqlite3.connect('class{}.db')
    print ("Opened database successfully");

    cursor = conn.execute("SELECT user_name,total_score, now from CLS1 ")
    for row in cursor:
       print ("user_name = ", row[0])
       print ("total_score = ", row[1])
       print ("date text = ", row[2], "\n")

    print ("Operation done successfully");
    conn.close()

Aucun commentaire:

Enregistrer un commentaire