Python 中的文本游戏问题。循环输入提示
Text game problems in Python. Looping input prompt
我的下一个问题是我的一个学生在他的基于文本的游戏中的代码。这个想法是,在游戏中你可以拿起一个物品并将它放在一个选定的口袋里。问题是,当他运行这段代码时,他得到了选择一个口袋来放入物品的提示,但随后输入提示只是循环出现关于将物品放在哪里的相同问题。
想法?
class grab():
#Checks the player's pockets for items.
def pocket_check(inventory, pocket, thing, input, state, list, room):
if inventory[pocket] != None:
print("You find that your %s is stuffed full.")%(input)
if inventory[pocket] == None:
inventory[pocket] = thing
del room["Items"][thing]
state["Strain"] += list[thing]["Weight"]
print("You put the %s in your %s.")%(thing, input)
#Takes items and putting them in the player's inventory
def grab(inventory, thing, list, state, room):
go = True
while go:
if list != "WEAPON" and state["Strain"]+list[thing]["Weight"] <= state["Strength"]:
inp = input("Where will you put it?").lower()
inp_broke = inp.split(" ")
if inp_broke[0] == "stop":
go = False
elif inp_broke[0] != "put":
inp = input("Where will you PUT it?").lower()
inp_broke = inp.split(" ")
elif inp_broke[0] == "put":
if inp_broke[1:3] == "shirt pocket":
pocket_check(inventory, "Shirt Pocket", thing, "shirt pocket", state, list, room)
if inp_broke[1:4] == "left front pocket":
pocket_check(inventory, "Left Front Pocket", thing, "left front pocket", state, "ITEM", room)
if inp_broke[1:4] == "right front pocket":
pocket_check(inventory, "Right Front Pocket", thing, "right front pocket", state, "ITEM", room)
if inp_broke[1:3] == "back pocket":
pocket_check(inventory, "Back Pocket", thing, "back pocket", state, "ITEM", room)
来自您的代码段:
np_broke = inp.split(" ")
...
if inp_broke[1:3] == "shirt pocket":
从 split() 分割列表 returns 字符串列表,而不是字符串:
>>> inp = "put shirt pocket"
>>> inp_broke = inp.split(" ")
>>> inp_broke[1:3]
['shirt', 'pocket']
这个
if inp_broke[1:3] == "shirt pocket":
行不通。 inp_broke
是一个(字符串的)列表,并不会神奇地组合成一个字符串。
您可以使用 str.join
方法:
if " ".join(inp_broke[1:3]) == "shirt pocket":
这会将列表的各个项目组合成一个字符串,由初始字符串分隔(在本例中为单个 space)。
while go
将永远循环,除非在循环中将 False
分配给 go
。 pocket_check
方法可以 return 一个布尔值,这取决于它的成功和调用时分配给 go
的 return 值。
我的下一个问题是我的一个学生在他的基于文本的游戏中的代码。这个想法是,在游戏中你可以拿起一个物品并将它放在一个选定的口袋里。问题是,当他运行这段代码时,他得到了选择一个口袋来放入物品的提示,但随后输入提示只是循环出现关于将物品放在哪里的相同问题。
想法?
class grab():
#Checks the player's pockets for items.
def pocket_check(inventory, pocket, thing, input, state, list, room):
if inventory[pocket] != None:
print("You find that your %s is stuffed full.")%(input)
if inventory[pocket] == None:
inventory[pocket] = thing
del room["Items"][thing]
state["Strain"] += list[thing]["Weight"]
print("You put the %s in your %s.")%(thing, input)
#Takes items and putting them in the player's inventory
def grab(inventory, thing, list, state, room):
go = True
while go:
if list != "WEAPON" and state["Strain"]+list[thing]["Weight"] <= state["Strength"]:
inp = input("Where will you put it?").lower()
inp_broke = inp.split(" ")
if inp_broke[0] == "stop":
go = False
elif inp_broke[0] != "put":
inp = input("Where will you PUT it?").lower()
inp_broke = inp.split(" ")
elif inp_broke[0] == "put":
if inp_broke[1:3] == "shirt pocket":
pocket_check(inventory, "Shirt Pocket", thing, "shirt pocket", state, list, room)
if inp_broke[1:4] == "left front pocket":
pocket_check(inventory, "Left Front Pocket", thing, "left front pocket", state, "ITEM", room)
if inp_broke[1:4] == "right front pocket":
pocket_check(inventory, "Right Front Pocket", thing, "right front pocket", state, "ITEM", room)
if inp_broke[1:3] == "back pocket":
pocket_check(inventory, "Back Pocket", thing, "back pocket", state, "ITEM", room)
来自您的代码段:
np_broke = inp.split(" ")
...
if inp_broke[1:3] == "shirt pocket":
从 split() 分割列表 returns 字符串列表,而不是字符串:
>>> inp = "put shirt pocket"
>>> inp_broke = inp.split(" ")
>>> inp_broke[1:3]
['shirt', 'pocket']
这个
if inp_broke[1:3] == "shirt pocket":
行不通。 inp_broke
是一个(字符串的)列表,并不会神奇地组合成一个字符串。
您可以使用 str.join
方法:
if " ".join(inp_broke[1:3]) == "shirt pocket":
这会将列表的各个项目组合成一个字符串,由初始字符串分隔(在本例中为单个 space)。
while go
将永远循环,除非在循环中将 False
分配给 go
。 pocket_check
方法可以 return 一个布尔值,这取决于它的成功和调用时分配给 go
的 return 值。