【发布时间】:2019-02-07 15:12:07
【问题描述】:
import random
string1 = "Fred"
string2 = "Jones"
int1 = 4
int2 = 8
randomInt = random.randint(1, 100)
GloriousListOfFun = [string1, string2, int1, int2, randomInt]
RandomFromGloriousListOfFun = random.choice(GloriousListOfFun)
x=0
while x<1:
line1 = "Hi I am {} from Albion".format(RandomFromGloriousListOfFun)
print(line1)
我知道这 99.9% 没用,但现在这会返回如下内容: “嗨,我是来自 Albion 的 9 岁” “嗨,我是来自 Albion 的 9 岁” “嗨,我是来自 Albion 的 9 岁” ETC... 有谁知道我怎样才能让它像这样重复,但每次都有一个随机变量?谢谢。
【问题讨论】:
-
您只使用了一次
random.choice。您需要在循环中再次使用它。试一试。 -
如果你想生成令人信服的随机数据来填充伪造的数据库进行测试或类似的事情,你可能想了解 Faker faker.readthedocs.io/en/latest/index.html