Python:在 if 语句中打印不是 运行
Python: Printing in if statements not running
我正在制作一个 python 程序,这是一个穿越鬼屋的冒险游戏。其中一件大事是一个名为 owned_items
的列表。当他们在房子里找到它们或在开始时由 random.choice
给他们时,这会得到由 "box of matches" 或 "torch" 等字符串表示的项目。有时,当他们面对一种情况时,他们的选择取决于他们是否有特定的项目。
我的代码:
#bullets is the variable for pistol ammo. During my testing of this function, it is at 5 when it should start
bullets=5
owned_items=[]
ronald_items=["a glass bottle", "a pistol", "a torch", "a small glass of oil", "a box of matches", "a can of spray paint", "a small knife", "a pair of surgical gloves", "a blessed amulet"]
owned_items.append(random.choice(ronald_items))
ronald_items.remove(owned_items[0]
owned_items.append(random.choice(ronald_items))
ronald_items.remove(owned_items[1])
#This part is in the actual definition where the problem appears when it should run
def skeleton_choice():
if "a glass bottle" in owned_items:
print('type "glass bottle" to attack the skeletons with that bottle')
if "a pistol" in owned_items and bullets>1:
print('type "shoot" to try firing your pistol at the skeletons')
if "a small knife" in owned_items:
print('type "small knife" to use your small knife against the skeletons')
if "a kitchen knife" in owned_items:
print('Type "kitchen knife" to use your kitchen knife against the skeletons')
if "a blessed amulet" in owned_items:
print('Type "amulet" to use the blessed amulet to make this a fairer fight')
print('Type "hands" to just fight the skeletons with your body')
print('Type "run" to try and get away from the skeletons')
即使我知道我在这些 if 语句中有 3 个项目,none 的印刷品也会出现。我使用 ifs 而不是 elifs 和 else 因为我希望它显示他们拥有的所有东西的印刷品,而不仅仅是一个。例如,如果他们有一个玻璃瓶和一把菜刀,我希望它能为他们提供瓶子和刀的打印报表。
你的代码对我有用。那是在我将调用添加到 skeleton_choice() 之后。可能是你没有调用它?
您没有在任何地方调用该函数,所以它不起作用。
只需添加:
skeleton_choice()
最后一行。也在行
ronald_items.remove(owned_items[0]
你少了一个括号。
我正在制作一个 python 程序,这是一个穿越鬼屋的冒险游戏。其中一件大事是一个名为 owned_items
的列表。当他们在房子里找到它们或在开始时由 random.choice
给他们时,这会得到由 "box of matches" 或 "torch" 等字符串表示的项目。有时,当他们面对一种情况时,他们的选择取决于他们是否有特定的项目。
我的代码:
#bullets is the variable for pistol ammo. During my testing of this function, it is at 5 when it should start
bullets=5
owned_items=[]
ronald_items=["a glass bottle", "a pistol", "a torch", "a small glass of oil", "a box of matches", "a can of spray paint", "a small knife", "a pair of surgical gloves", "a blessed amulet"]
owned_items.append(random.choice(ronald_items))
ronald_items.remove(owned_items[0]
owned_items.append(random.choice(ronald_items))
ronald_items.remove(owned_items[1])
#This part is in the actual definition where the problem appears when it should run
def skeleton_choice():
if "a glass bottle" in owned_items:
print('type "glass bottle" to attack the skeletons with that bottle')
if "a pistol" in owned_items and bullets>1:
print('type "shoot" to try firing your pistol at the skeletons')
if "a small knife" in owned_items:
print('type "small knife" to use your small knife against the skeletons')
if "a kitchen knife" in owned_items:
print('Type "kitchen knife" to use your kitchen knife against the skeletons')
if "a blessed amulet" in owned_items:
print('Type "amulet" to use the blessed amulet to make this a fairer fight')
print('Type "hands" to just fight the skeletons with your body')
print('Type "run" to try and get away from the skeletons')
即使我知道我在这些 if 语句中有 3 个项目,none 的印刷品也会出现。我使用 ifs 而不是 elifs 和 else 因为我希望它显示他们拥有的所有东西的印刷品,而不仅仅是一个。例如,如果他们有一个玻璃瓶和一把菜刀,我希望它能为他们提供瓶子和刀的打印报表。
你的代码对我有用。那是在我将调用添加到 skeleton_choice() 之后。可能是你没有调用它?
您没有在任何地方调用该函数,所以它不起作用。 只需添加:
skeleton_choice()
最后一行。也在行
ronald_items.remove(owned_items[0]
你少了一个括号。