我在空闲时间开发基于文本的RPG以获得乐趣,并且我已经开始为NPC互动实施易货,盗窃和购物系统。交易工作和偷窃一样,但购买游戏内物品却没有。所有的机制工作,但由于某种原因,for循环运行两次!我甚至尝试实现一个整数来限制它购买,但它也没有帮助。我提供了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
以下是运行文件时发生的情况:
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
随意询问是否有足够的信息。
答案 0 :(得分:0)
从原帖:
问题是递归导入。如果您遇到同样的问题,请检查您的导入。