简体   繁体   English

我的sqlite怎么了?

[英]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()

You cast to int then compare to strings inside the list so the if could never evaluate to True 您将其intint然后与列表中的字符串进行比较,因此if永远无法求值为True

class_number = int(input('Please enter your class number: '))

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

remove the int: 删除int:

class_number =  input('Please enter your class number: ')

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

You should also use range instead of your while loop: 您还应该使用range而不是while循环:

import random
for _ in range(10)

The logic for you sqlite also seems a bit off: sqlite的逻辑似乎也有些偏离:

import sqlite3
import os

if class_number in ['1', '2', '3']:
    db = 'class{}.db'.format(class_number)
    query = """
         INSERT INTO ClS1    VALUES
              (?, ?, ?)
            """
    if not os.path.isfile(db):
        conn = sqlite3.connect(db)
        c = conn.cursor()
        c.execute('''CREATE TABLE CLS1  (USERNAME TEXT, SCORE INT, DATE TEXT);''')
        conn.execute(query, (user_name, total_score, now))
        conn.commit()

    else:
        conn = sqlite3.connect(db)
        print("Opened database successfully")
        conn.execute(query, (user_name, total_score, now))
        conn.commit()
        cursor = conn.execute("SELECT USERNAME, SCORE, DATE 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM