在python函数中遇到while循环问题

时间:2013-02-16 14:19:45

标签: python

我有一个小应用程序要求电影。当评级超过50时,请返回观看。如果没有,请选择另一部电影。虽然当我达到movieDec中的条件时,它不会回到函数的顶部来计算电影的分数。无法想出这个。

def getScore():
   choose = raw_input("pick a movie ")
   rt = RT()
   info = rt.search(choose)
   rating1 = info[0].get('ratings').get('critics_score')
   rating2 = info[0].get('ratings').get('audience_score')
   stars = (rating1 + rating2) / 2
   return rating1, rating2, stars


def movieDec():
   critic, aud, stars = getScore()
   print 'Critics gave it a %s' %critic
   print 'Audiences gave it a %s' %aud
   print 'The average rating is %s' %stars
   while stars < 50:
       print "That's no good, pick again"
       getScore()
    print 'Good choice.'

3 个答案:

答案 0 :(得分:2)

您没有更新循环中的stars变量。尝试:

while stars < 50:
   print "That's no good, pick again"
   critic, aud, stars = getScore()

答案 1 :(得分:0)

在循环中,您忽略了getScore()的返回值:

   getScore()

将其更改为:

   critic, aud, stars = getScore()

答案 2 :(得分:0)

此更正后的代码above将第二次调用getScore()函数(如果星标<50),但在循环显示之前它将不显示分数:

while stars < 50:
   print "That's no good, pick again"
   critic, aud, stars = getScore()
   print 'Critics gave it a %s' %critic
   print 'Audiences gave it a %s' %aud
   print 'The average rating is %s' %stars