当我运行代码时,程序运行时没有错误,但在询问“班级号码”后留下空格。我无法说出错误。我还没有完成代码,所以只是想知道它是否有效,但它还没有能够达到目标。
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()
答案 0 :(得分:1)
您转换为int
然后与列表中的字符串进行比较,以便if永远不会评估为True
class_number = int(input('Please enter your class number: '))
if class_number in ['1','2','3']:
删除int:
class_number = input('Please enter your class number: ')
if class_number in ['1','2','3']:
您还应该使用范围而不是while循环:
import random
for _ in range(10)
你的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()