python - 如何让 Python 在两个选项之间做出选择?

标签 python random

所以,我最近一直在用 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/

相关文章:

python - 有没有办法用 Python(或其他语言)确定 JPG 图像是否损坏?

python - 如何将annotation.xml文件转换为train.txt和val.txt以进行对象检测?

python - 如何正确实现复位功能?

java - 如何在不将脚本存储在 .py 文件中的情况下从 java 运行 python 脚本?

c# - 生成随机单位

c - `clear` 导致 undefined reference 错误

r - 生成的随机数的卡方拟合优度

python - 基于Key对Python字典进行排序?

java - 为什么我得到一个数组越界异常,为什么我只生成数字 0?

algorithm - 如何生成具有单位长度列的随机 n*y 矩阵?