Python while循环不会继续

时间:2016-04-25 14:16:06

标签: python linux ubuntu while-loop pycharm

程序应该继续要求用户输入标题并在答案时回答#34;你想继续吗(是/否)?"是Y.相反它只询问用户一次。有人可以帮助我弄清楚循环不会继续。谢谢!

#!/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()

1 个答案:

答案 0 :(得分:0)

你有一个错误的缩进行“return 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