2016-04-25 226 views
0

程序應該不斷詢問用戶輸入標題並查看「你想繼續(是/否)」的答案嗎?是Y.相反,它只詢問用戶一次。有人可以幫我弄清楚,而循環不會繼續。謝謝!Python循環不會繼續

#!/usr/bin/env python 

def main(): 

listOfReviews = obtainListOfReviews() 
displayResults(listOfReviews) 

def obtainListOfReviews(): 
listOfReviews = [] 
carryOn ='Y' 
while carryOn =='Y': 
    title = raw_input("Enter title:") 
    review = int(input("Enter review:")) 

    st = MovieReview(title, review) 
    listOfReviews.append(st) 
    carryOn = raw_input("Do you want to continue?(Y/N)? ") 
    carryOn = carryOn.upper() 
    return listOfReviews 

def displayResults(listOfReviews): 
    for r in listOfReviews: 
     print(r) 

class MovieReview: 
def __init__(self, title = " ", review = 0): 
    self._title = title 
    self._review = review 
def setTitle(self, title): 
    self._title = title 
def setReview(self, review): 
    self._review = review 
def getTitle(self): 
    return self._title 
def getReview(self): 
    return self._review 
def __str__(self): 
    return ("Title: " + str(self._title) + " \nreview: " + str(self._review)) 
main() 
+0

因爲你'return' while循環 – JCVanHamme

+0

你'return'是'while'環路的一部分。 Unindent它。 – Matthias

+0

'return listOfReviews'使函數'obtainListOfReviews()'返回存儲在'listOfReviews'中的值,並將其分配給'main()'中的變量'listOfReviews'。 –

回答

0

你有一個錯誤的縮進你行 「迴歸listOfReviews」:

def obtainListOfReviews(): 
listOfReviews = [] 
carryOn ='Y' 
while carryOn =='Y': 
    title = raw_input("Enter title:") 
    review = int(input("Enter review:")) 

    st = MovieReview(title, review) 
    listOfReviews.append(st) 
    carryOn = raw_input("Do you want to continue?(Y/N)? ") 
    carryOn = carryOn.upper() 

# Correct indentation 
return listOfReviews