在Python 2.7

时间:2018-01-24 15:58:06

标签: python

我正在学习python 2.7,我正在尝试完成编程的基础以继续使用python 3.0。我从我的书“学习python”中看到了这段代码 - Zed A. Shaw:

from sys import exit
from random import randint

class Game(object):
    def __init__(self, start):
        self.quips =[
        "You died. You kidda suck at this."
        "Your mom would be proud.If she were smarter."
        "Such a luser."
        "I have a small puppy that's better at this."

         ]
        self.start = start
    def play(self):
        next = self.start

    while True:
        print "\n-------"
        room = getattr(self, next)
        next = room() 

    def central_corridor(self)
    [ some other functions ]

    def death(self):
    print self.quips[randint(0, len(self.quips)-1)]
    exit(0)

a_game = Game("central_corridor")
a_game.play()

什么是“下一个”,我在编辑器中看到它是蓝色的,是“下一个”特别的东西?我从开头就忘记了程序的流程(“next = self.start”最让我困惑),请帮助我。

2 个答案:

答案 0 :(得分:1)

next是在迭代器中使用的内置函数的名称。如果您想在使用中看到它,DataCamp有一个很好的解释:https://www.programiz.com/python-programming/methods/built-in/next

它在您的示例中用作变量,但IDE会将其突出显示。与list是函数

时使用list()作为变量相同

答案 1 :(得分:0)

nextbuild-in python function的名称。您的IDE很可能因此而着色。在示例代码的上下文中,它看起来只是一个具有相同名称的变量。