Python3:在一行中返回带有字符串的random.randint

时间:2018-10-26 20:05:36

标签: string random python-3.6

import random
for x in range(5):
a = (random.randint(1,71))
b = "your numbers are"
c = str(a)
    print(b + c)

如何获取输出“您的数字是x1,x2,x3,x4,x5”?

1 个答案:

答案 0 :(得分:2)

我将使用字符串连接(下面的表达式,例如“ b + =“而不是“ b =”))来解决这样的问题:

import random

b = ""
for x in range(5):
   a = (random.randint(1,71))
   b += str(a)
   # this part is only important if you care about having commas between numbers
   if x < 4:
      b += ", "
print("your numbers are " + b)