所以,我最近一直在用 Python 编写代码并且它一直运行良好,直到我想让 Python 在两种不同的结果之间进行选择。 似乎不起作用的代码部分如下所示:
line = ( (random.choice(str_a) + \
random.choice(str_b) + \
random.choice(str_c) + \
random.choice(str_d) + \
random.choice(str_e) + \
random.choice(str_f)) or \
((str_g) + (random.choice(str_h)) )
str_a, b, c, d, e, f, g, h
都是包含多个单词的列表。
现在我希望 Python 在 a-f
组合或 f,g
组合之间随机选择。
出于某种原因,我没有收到任何错误,但 Python 一直选择第一个选项。
提前致谢!
最佳答案
为什么不再使用选择?
line = random.choice([random.choice(str_a) + random.choice(str_b) + random.choice(str_c) + random.choice(str_d) + random.choice(str_e) + random.choice(str_f)), (str_g) + (random.choice(str_h)])
关于python - 如何让 Python 在两个选项之间做出选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258569/