Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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 for循环运行两次,即使有中断_Python_For Loop - Fatal编程技术网

Python for循环运行两次,即使有中断

Python for循环运行两次,即使有中断,python,for-loop,Python,For Loop,我在空闲时间玩一个基于文本的RPG游戏,我已经开始为NPC互动实施易货、偷窃和购物系统。以物易物和偷窃一样有效,但用游戏内的钱购买物品却不行。所有的机械工作,但出于某种原因,循环运行两次!我甚至尝试实现一个整数来限制一次购买,但也没有任何帮助。我已经为selltoplayer()函数提供了代码 请记住,除了在中断后继续循环外,其他一切都在工作 def selltoplayer(self, player): inv = self.readableinv() #Give us a view

我在空闲时间玩一个基于文本的RPG游戏,我已经开始为NPC互动实施易货、偷窃和购物系统。以物易物和偷窃一样有效,但用游戏内的钱购买物品却不行。所有的机械工作,但出于某种原因,循环运行两次!我甚至尝试实现一个整数来限制一次购买,但也没有任何帮助。我已经为
selltoplayer()
函数提供了代码

请记住,除了在中断后继续循环外,其他一切都在工作

def selltoplayer(self, player):
    inv = self.readableinv()  #Give us a viewable inventory
    print("What would you like? I have the following items in stock:\n{0}".format(inv))  #display said inv
    purchaseitem = input("Type the name of the item that you wish to purchase:\n")  #Get the requested item
    timespurchased = 0
    for item in self.inventory:  #search each item in the inventory
        if timespurchased < 1:
            if item.name == purchaseitem:  #once the correct item is found...
                ImportantCalculations.determinevalue(player, item)  #get the item's value
                if player.money >= item.buyvalue:  #and make sure that the player can afford it.
                    confirm = input("So, you would like to purchase {0} for {1} Karics?\n".format(item.name,
                                                                                              item.buyvalue))
                    #Just a simple confirmation
                    if confirm.lower() == 'y' or confirm.lower() == 'yes':  #If the player would like the item
                        print("Thanks for your purchase!")  #the merchant is kind
                        player.inventory.append(item)  #put the item in the player's inventory
                        self.inventory.remove(item)  #take the item out of the merchant's inventory
                        player.money -= item.buyvalue  #withdraw the proper amount of money from the player's stores
                        pinv = player.readableinv()  #Readable inventory for player
                        print(pinv)  #Debug line to make sure that the item was properly transferred
                        print(player.money)  #Debug line to make sure that the money was properly transferred
                        timespurchased += 1
                        break  #stop
                    elif confirm.lower() == 'n' or confirm.lower() == 'no':  #If the player made a mistake
                        print("Oh, okay.")  #Try to guilt the player out of leaving
                        exitsell = input("Would you like to browse again (A) or exit (B)?\n")  #ask if they want to
                        #purchase something different or leave
                        if exitsell.lower() == 'a':  #resell items
                            self.rselltoplayer(player)
                        elif exitsell.lower() == 'b':  #exit
                            print("Thanks for stopping by!")
                            break
                        else:
                            print("Sorry, I don't speak gibberish. I'll see you around!")
                            break
                    else:
                        print("Sorry, I don't speak gibberish. I'll see you around!")
                        break
                else:
                    print("I'm sorry, but you cannot afford this...")
                    self.rselltoplayer(player)
        else:
            break

如果没有足够的信息,请随时询问。

原文:

问题是递归导入。如果您有相同的问题,请检查您的导入


重要的计算是从哪里得到的,当我做一个简单的复制和粘贴时,程序不会运行。
self.rselltoplayer(player)
do做什么?还有,这是从哪里调用的?@danidee我将用控制台输出更新(因为它引用了许多文件和对象)@Dannnno
self.rselltoplayer(player)
只是一个“重置”功能。它调用
selltoplayer(播放器)
What would you like? I have the following items in stock:

['Stick', 'Rock', 'Scarf', 'Explosives', 'Diamond Ring']  #Prints inventory fine

Type the name of the item that you wish to purchase:

Stick

So, you would like to purchase Stick for 104 Karics?    #Interprets player choice properly (including the value)

yes

Thanks for your purchase!  #Interprets confirmation fine

['Stick']  #Prints player inventory fine

396  #Prints player money fine

#Then... It loops again.

What would you like? I have the following items in stock:

['Rock', 'Scarf', 'Explosives', 'Diamond Ring']

Type the name of the item that you wish to purchase:

Scarf

#And doesn't even complete. What's going on?

Process finished with exit code 0