2 回答

TA貢獻1784條經驗 獲得超9個贊
input
是一個函數,返回用戶輸入的值。
if input != 'exit':
永遠為真,因為input
是一個函數,并且永遠不會等于 'exit'
您需要檢查輸入的返回值以查看它是否與字符串“exit”匹配。
編輯:嘗試以下操作- 如果您有更多提示或沒有提示,則此選項應該是“可擴展的”。但有很多方法可以實現您想要做的事情。以下只是其中之一。我添加了評論,因為您似乎是 python 新手!
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
cursor = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS catalog (
? ? ? ?number integer NOT NULL PRIMARY KEY autoincrement,
? ? ? ?type text NOT NULL,
? ? ? ?taxon text,
? ? ? ?species text NOT NULL,
? ? ? ?part text NOT NULL,
? ? ? ?age integer,
? ? ? ?layer text,
? ? ? ?notes text
? ? ? ?)""")
while True:
? ? ? print('Please enter individual specimen data: ')
? ? ? input_prompts = [
? ? ? ? 'Catalog #: ',
? ? ? ? 'Type of Specimen: ',
? ? ? ? 'Taxon: ',
? ? ? ? 'Species: ',
? ? ? ? 'Body Part: ',
? ? ? ? 'Estimated Age: ',
? ? ? ? 'Sedimentary Layer: ',
? ? ? ? 'Notes: '
? ? ? ]
? ? ? responses = []
? ? ? response = ''
? ? ? for prompt in input_prompts: # loop over our prompts
? ? ? ? response = input(prompt)
? ? ? ? if response == 'exit':
? ? ? ? ? break # break out of for loop
? ? ? ? responses.append(response)
? ? ??
? ? ? if response == 'exit':
? ? ? ? break # break out of while loop
? ? ? # we do list destructuring below to get the different responses from the list
? ? ? c_number, c_type, c_taxon, c_species, c_part, c_age, c_layer, c_notes = responses
? ? ? cursor.execute("""
? ? ? ? ? INSERT OR IGNORE INTO catalog(number, type, taxon, species, part, age, layer, notes)
? ? ? ? ? VALUES (?,?,?,?,?,?,?,?)
? ? ? ? ? """,
? ? ? ? ? ? ? ? ? ? ?(
? ? ? ? ? c_number,
? ? ? ? ? c_type,
? ? ? ? ? c_taxon,
? ? ? ? ? c_species,
? ? ? ? ? c_part,
? ? ? ? ? c_age,
? ? ? ? ? c_layer,
? ? ? ? ? c_notes,
? ? ? ? ? ))
? ? ? conn.commit()
? ? ? responses.clear() # clear our responses, before we start our new while loop iteration
? ? ? print('Specimen data entered successfully.')
c.execute("""CREATE VIEW catalog
AS
SELECT * FROM catalog;
""")
conn.close()

TA貢獻1876條經驗 獲得超6個贊
如果沒有詳細介紹您的代碼,我相信它會因識別錯誤而失?。?/p>
python 要求您在 while 循環內縮進代碼,因此它應該如下所示。
while True:
if input != 'exit':
print("Please enter individual specimen data: ")
...
第二個問題是您從未創建過該變量input。當你測試時if input == 'exit':它會失敗。您需要決定用戶在哪一點可以選擇鍵入exit,將其保存到變量中并測試它,大致如下(我調用變量userinput是為了不覆蓋input函數):
while True:
userinput = input( 'type exit to exit' )
if userinput != 'exit':
print("Please enter individual specimen data: ")
...
添加回答
舉報