我正在 python 3.3.2 中创建基于文本的游戏,我想根据攻击未命中或命中(随机选择)后发生的情况显示一条消息,根据发生的情况,您会收到不同的消息。这是到目前为止的代码
print ("A huge spider as large as your fist crawls up your arm. Do you attack it? Y/N")
attack_spider = input ()
#North/hand in hole/keep it in/attack
if attack_spider == "Y":
attack = ['Miss', 'Miss', 'Miss', 'Miss', 'Hit']
from random import choice
print (choice(attack))
我认为它看起来像这样:
if attack == 'Miss':
print ("You made the spider angry")
但这似乎不起作用。是否有可能做到这一点?
我在下面的答案中添加了代码,如下所示:
if attack_spider == "Y":
attack = ['Miss', 'Miss', 'Miss', 'Miss', 'Hit']
from random import choice
print (choice(attack))
messages = {
"Miss": "You made the spider angry!",
"Hit": "You killed the spider!"
}
print messages[choice(attack)]
但知道当我运行程序时,我会收到如下错误:
语法错误并突出显示消息
我只是添加了错误的代码还是其他的东西