如何对不同的随机反应采取行动(在我的文字冒险游戏中)

问题描述 投票:0回答:1

我在python中制作一个文本冒险游戏,我已经随机化了对我的每个首选(A,B,C)的回复,现在让我们说他在选择A时没有找到任何东西。我怎么能基本上说;

“如果没有找到,(*在此处插入另一个选项列表*)”

进入我的代码?我似乎找不到答案,所以我希望有人知道,谢谢!到目前为止,这是我的代码:

import time
import random

print ('You suddenly jolt up from the sleeping bag, awoken by the howling of nearby wolves.\nYou are cold, shivering and starving, and the few things that are still visible,\nare lit up only by the moon, and notice you are in the middle of a forest.\n')
ch1 = str(input(' Do you: \nA: Use your hands to search around\nB: Sleep again until morning\nC: Get up, and attempt to run away from the wolves in the dark:\n'))

responsesA = [
    'You found a torch!',
    'You did not find anything.'
    ]

responsesB = [
    'You fell asleep again cold and hungry.',
    'You are unable to sleep.'
    ]

responsesC = [
    'You start running as fast as you can and successfuly escape!',
    'You trip and injure knee.'
    ]


if ch1 in ['A']:
    print (random.choice(responsesA))

if ch1 in ['B']:
    print (random.choice(responsesB))

if ch1 in ['C']:
    print (random.choice(responsesC))

有什么我应该改变的吗?

python python-3.x python-3.6
1个回答
0
投票

这个怎么样?

if ch1 in ['A']:
    random_value = random.choice(responsesA)
    print(random_value)
    if random_value == 'You did not find anything.':
       # insert another list of choices here

而不是打印出随机值,将其保存在变量中,然后检查随机值是否为“无值”并相应地处理它。

© www.soinside.com 2019 - 2024. All rights reserved.