剛開始,我只是一個初學者。我知道的很多東西,在過去的兩天裏我從互聯網上了解到。現在解決這個問題。在從Python中使用的sqlite表中拉取項目時遇到問題
我正在開發一個項目,其目標是在Python中開發基於文本的瑣事遊戲。我正在使用sqlite3數據庫來存儲問題的信息。數據庫佈局由三列組成,第一列包含一個ID號,第二列包含一個包含問題和答案選項的文本字符串,第三列包含相應問題的答案。
我可以很容易地獲得要創建的列表並將信息塊拉出來,但是當我需要一次拉一行時會出現問題。爲了獲得最大的清晰度,這是我的代碼完全:
#imports necessary SQL connection files
import sqlite3
import sys
#Data to form the Question table
info = (
("Who invented the Golden Meme? A:Copericus B:John Cena C:Aristotle D:Shie Lebeouf", "C"),
("What is the best chip flavor? A:Sour Cream & Onion B:Salt & Vinegar C:Barbecue D:Moldy", "B"),
("Who will be prisident in 2017? A:Donald Trump B:Bernie Sanders C:Hillary Clinton D:Ben Carson", "D"),
("Why? A:Becase he's smart and well educated B:Because he's a doctor C:Because once you go black you never go back D:Because my IQ is less than 30", "C")
)
#Connects to the SQL database
con = sqlite3.connect('Questions.db')
cur = con.cursor()
#Deletes the Questions table if it already exists, then creates a new one containing all the data
with con:
cur.execute('''DROP TABLE IF EXISTS Questions''')
cur.execute('''CREATE TABLE Questions(ID INTEGER PRIMARY KEY, question TEXT, answer TEXT)''')
cur.executemany('''INSERT INTO Questions(question, answer) VALUES(?, ?)''', info)
con.commit()
#Prints instructions
def instructions():
print()
print("INSTRUCTIONS: When a question is displayed, type the letter that corresponds to the desired answer choice. Type using capital letters only. If you answer a question wrong, you will recieve a Failure message and the game will end. If you answer all questions correctly, you win.")
print()
print()
#Displays which question the player is on
def counter():
global n
print("Question#", n)
n = n+1
nextQuestion()
#Displays the next question and recieves the players answer choice
def nextQuestion():
cur.execute('''SELECT question FROM Questions''')
Q = cur.fetchone()
if Q == None:
print()
print("Victory!")
print()
return False;
else:
print (Q)
playerAnswer = str(input("Your Answer: "))
answerValidation(playerAnswer)
#Determines is the answer is correct and, if so, restarts the process
def answerValidation(playerAnswer):
cur.execute('''SELECT answer FROM Questions''')
B = cur.fetchone()
if playerAnswer == B:
print()
print("Correct!")
print()
counter()
else:
print()
print ("You Failed!")
print(B)
return False
n = 1
instructions()
counter()
的問題是,我可以打印第1個問題,但不是以後的任何問題。我的印象是cur.fetchone()
想要獲取當前行的行,然後繼續下一行,但是當代碼第二遍傳遞時,它只是重新打印第一個問題。
這是與該問題有關的部分。
def nextQuestion():
cur.execute('''SELECT question FROM Questions''')
Q = cur.fetchone()
if Q == None:
print()
print("Victory!")
print()
return False;
else:
print (Q)
還有第二個問題。我還使用cur.fetchone()
系統來提出相應的答案。它確實獲得了正確的答案,雖然也許只是第一個答案,但它提取的答案仍然與表格中的格式相同。我在打印B行中查看它給我的答案,結果是('C',)
。我認爲這就是我放入任何答案總是以虛假結尾的原因。即使我在C中輸入了正確的答案,它仍然算錯了,很可能是因爲它從表格中得出的答案中有這些撇號,括號和逗號。
如果我更改了代碼,以便playerAnswer in ['C','c']:
它將計算答案C正確並通過程序運行回來再次提出第一個問題。
代碼有關回答的問題:
playerAnswer = str(input("Your Answer: "))
answerValidation(playerAnswer)
#Determines is the answer is correct and, if so, restarts the process
def answerValidation(playerAnswer):
cur.execute('''SELECT answer FROM Questions''')
B = cur.fetchone()
if playerAnswer in ['C','c']:
print()
print("Correct!")
print()
counter()
else:
print()
print ("You Failed!")
print(B)
return False
概括起來講,三個主要問題:
- 我不能讓程序在每次打印一個問題,然後繼續下一個問題只有在給出正確答案的情況下。
- 我無法讓程序接受與其相應問題對應的正確答案。
- 我不能讓程序打印的問題和答案的文本及其存儲的
('X',)
的任何解決方案,想法,或幫助將不勝感激格式之外。
還應該指出,我設計的程序是爲了順序地完成問題(主要是因爲我認爲它會更容易),但它不必這樣工作。如果任何人都可以提供解決方案來解決上述問題,但隨機選擇問題,我也非常感謝。它必須是可擴展的,不能多次顯示問題。
謝謝!