Reputation: 13
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()
Upvotes: 0
Views: 70
Reputation: 180441
You cast to int
then compare to strings inside the list so the if could never evaluate to True
class_number = int(input('Please enter your class number: '))
if class_number in ['1','2','3']:
remove the 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:
import random
for _ in range(10)
The logic for you sqlite also seems a bit off:
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()
Upvotes: 1