Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python文本冒险的翻转计数器_Python - Fatal编程技术网

python文本冒险的翻转计数器

python文本冒险的翻转计数器,python,Python,我已经编写了一个python文本冒险游戏,我想添加的最后一件事是一个计数器,用于计算在游戏结束时显示的轮次数 它只需要计算每次玩家输入的东西,但我不知道如何编码,这有点尴尬,因为我相信这将是一个非常简单的解决方案 我正在使用python 3.4.1 while True: playerInput = input("What do you want to do? ") playerInput = playerInput.lower() playerWords = playe

我已经编写了一个python文本冒险游戏,我想添加的最后一件事是一个计数器,用于计算在游戏结束时显示的轮次数

它只需要计算每次玩家输入的东西,但我不知道如何编码,这有点尴尬,因为我相信这将是一个非常简单的解决方案

我正在使用python 3.4.1

while True:
    playerInput = input("What do you want to do? ")
    playerInput = playerInput.lower()
    playerWords = playerInput.split(" ", 1)
    verb = playerWords[0]
    if len(playerWords) == 2:
        noun = playerWords[1]
    else:
        noun = ""


    if playerInput == "quit":
        break



    elif playerInput == "look":
        print(roomDescriptions[currentRoom])



    ##--Controls movement--##             
    elif playerInput in dirs:
        playerInput = playerInput[0]
        if "treasure" in invItems and playerInput == "s" and currentRoom == "strangeWall":##--Checks for treasure in inventory before allowing game to be won--##
            print("!!!!Congratulations you have escaped from the dark dungeon!!!!")
            break

        elif playerInput in roomDirections[currentRoom]:
            currentRoom = roomDirections[currentRoom][playerInput]
            print(roomEntrance [currentRoom])
        else:
            print("You can't go that way")



    elif playerInput == "lookdown":##--checks for room items on the ground--##
        printList ("You see;", roomItems[currentRoom])



    elif playerInput == "inventory" or playerInput == "inv":##--Displays inventory items--##
        printList ("You are carrying;", invItems)



    elif verb == "get":##--Controls picking up items and adding them to inventory/removes from room--##
        if noun in roomItems[currentRoom]:
            print("picked up", noun)
            invItems.append(noun)
            roomItems[currentRoom].remove(noun)
        else:
            print("There is nothing to pick up")



    elif verb == "drop":##--Controls dropping items and removing them from the inventory/adds to room items--##
        if noun in invItems:
            print("You drop the", noun)
            roomItems[currentRoom].append(noun)
            invItems.remove(noun)
        else:
            print("You are not carrying", noun)


    elif verb == "use":##--Controls using the lamp and snow boots--##
        if noun in invItems:##--Checks inventory for lamp or snowboots before allowing certain directional movement--##
            if noun == "lamp":
                print("You light the lamp")
                invItems.remove(noun)
                roomDirections["hallMid"]["e"] = "giantNature"

            elif noun == "snowboots":
                print("You put on the snowboots")
                invItems.remove(noun)
                roomDirections["hallMid"]["s"] = "snowRoom"
            else:
                print("You cannot use that")
        else:
            print("You do not have", noun)





    else:
        print ("I don't understand")

如果没有看到您的代码示例,就几乎不可能告诉您任何特定的代码

class CountedInput(object):
    def __init__(self):
        self.counter = 0
    def input(self, *args):
        self.counter += 1
        return input(*args)

counted_input = CountedInput()
但是我可以给你一些通用的东西,你可以根据你的代码进行调整

class CountedInput(object):
    def __init__(self):
        self.counter = 0
    def input(self, *args):
        self.counter += 1
        return input(*args)

counted_input = CountedInput()
现在,在代码中调用
input()
的任何地方,都可以调用
counted\u input.input()

当你想显示转弯计数器时,那只是
计数的\u输入。计数器

(如果您使用的是Python2.x,请将
input
更改为
raw\u input


现在,您已经为问题添加了一个示例:

这个建议可以很好地发挥作用,但你可以让事情变得更简单

你的整个游戏都是围绕着一个命令循环进行的。每个循环只调用一次
input
。所以,你需要做的就是数一数你绕这个循环走了多少次。您可以这样做:

counter = 0
while True:
    counter += 1
    playerInput = input("What do you want to do? ")
    # all the rest of your code
现在,您只需打印或使用与任何其他变量相同的
计数器。例如:

    elif playerInput == "score":
        print("You have 0/0 points after", counter, "turns")
global turn_counter
turn_counter = 0
(我猜,当你不记分时,你实际上不想用
得分
命令来控制你的玩家,但这应该会显示出你的理想。)


如果你想变得聪明,有一个更简单的方法来做到这一点:只需循环从1到无穷大的所有数字。怎么用?该函数的工作方式类似于
范围
,只是没有
停止
值,因为它从不停止:

from itertools import count

for counter in count(1):
    # the rest of your code

我知道很多人可能不喜欢这个想法,因为我看到了关于全局变量使用的相互矛盾的观点,但是我会使用一个全局变量来存储圈数,并使用一个全局函数来跟踪它

例如:

    elif playerInput == "score":
        print("You have 0/0 points after", counter, "turns")
global turn_counter
turn_counter = 0
然后,当采取行动时,您可以:

turn_counter += 1
但是,我认为您需要在您的功能中包含全局

例如:

def game_input_handler():
    """ handles all user input """
    global turn_counter

    // game prompt code here
    // if..elif...else for option handling

哦,对不起,我没想到你会需要看,我还是个孩子newbie@kebab:没问题。这将是一个好主意,阅读本网站上的帮助;关于如何提出好的问题,这里有一些很好的信息。但与此同时,我已经更新了我的答案来处理你更具体的问题(正如你所看到的,这种方式简单得多,这也是问更具体的问题很好的部分原因)。是的,我只有第三天使用该网站,也是python的第一学期,所以我非常感谢你在我学习的时候仍然和我赤裸裸地交流,非常感谢您在看到我的代码完美运行后的第一个建议,非常感谢@烤肉串:这个网站上的大多数人对没有兴趣学习烤肉串工作原理的新手非常失望,他们很乐意接受任何明显想像你一样学习的人。:)