2014-04-07 131 views
0

我是一個新手程序員,但我通常可以發現這樣的事情,所以我想它是一個邏輯錯誤,而不是語法之一。所以我想知道你是否可以用你的眼睛來糾正錯誤。代碼是:不會離開while循環

database = open("database.txt", "r+") 
databaselist = database.readlines() 
length = len(databaselist) 
for i in range (length): 
    database.readline() 

Continue = True 
while Continue == True: 
    Title = input("Enter title of book: ") 
    Author = input("Enter author of book: ") 
    Genre = input("Enter genre of book: ") 
    Location = input("Enter the location of the book: ") 
    TitleWrite = Title + "\n" 
    AuthorWrite = Author + "\n" 
    GenreWrite = Genre + "\n" 
    LocationWrite = Location + "\n" 
    database.write(str(TitleWrite)) 
    database.write(str(AuthorWrite)) 
    database.write(str(GenreWrite)) 
    database.write(str(LocationWrite)) 
    Continue2 = input("Would you like to continue? Y or N: ") 
    if Continue2 == "n": 
     Contine = False 
     database.close() 
+3

Contine - >繼續 – PascalVKooten

+0

錯字'Contine'是一個不同於'Continue'的參數 – CoryKramer

回答

2

您可以使用break語句打破循環。利用這一點,你可以擺脫Continue變量:

while True: # "infinite" loop that you will break out of 
    title = ... 
    author = ... 

    response = input('Would you like to continue? Y or N: ') 
    if response.lower() == 'n': 
     database.close() 
     break # break out of the "infinite" loop 

(請注意,在Python中,規範是有小寫的變量名)。

+0

謝謝生病了,我相信它會起作用。 PS - 我知道它的正常做.lower(),但我調試時刪除它,只是沒有把它放回 – GavinHenderson5