我正在尝试编写一个脚本,该脚本接收许多所需的随机数作为输入,然后生成并打印它们。
但是,我的脚本将数字相加,而不是连接字符串。我想让字符串加入,这样它就会生成引脚,如下所示:
输入要生成的午餐馅饼数量: 10
26141
128111
937502
2436
56516
83623
246317我的代码:
import random
PTG = int(input("Enter the amount of pins to generate: \n"))
PG = 0
PS = ""
while PTG > PG:
RN1 = random.randint(0, 9)
RN2 = random.randint(0, 9)
RN3 = random.randint(0, 9)
RN4 = random.randint(0, 9)
RN5 = random.randint(0, 10)
RN6 = random.randint(0, 10)
if RN1 == 0:
PS += "0"
elif RN1 == 1:
PS += "1"
elif RN1 == 2:
PS += "2"
elif RN1 == 3:
PS += "3"
elif RN1 == 4:
PS += "4"
elif RN1 == 5:
PS += "5"
elif RN1 == 6:
PS += "6"
elif RN1 == 7:
PS += "7"
elif RN1 == 8:
PS += "8"
elif RN1 == 9:
PS += "9"
elif RN2 == 0:
PS += "0"
elif RN2 == 1:
PS += "1"
elif RN2 == 2:
PS += "2"
elif RN2 == 3:
PS += "3"
elif RN2 == 4:
PS += "4"
elif RN2 == 5:
PS += "5"
elif RN2 == 6:
PS += "6"
elif RN2 == 7:
PS += "7"
elif RN2 == 8:
PS += "8"
elif RN2 == 9:
PS += "9"
if RN3 == 0:
PS += "0"
elif RN3 == 1:
PS += "1"
elif RN3 == 2:
PS += "2"
elif RN3 == 3:
PS += "3"
elif RN3 == 4:
PS += "4"
elif RN3 == 5:
PS += "5"
elif RN3 == 6:
PS += "6"
elif RN3 == 7:
PS += "7"
elif RN3 == 8:
PS += "8"
elif RN3 == 9:
PS += "9"
elif RN4 == 0:
PS += "0"
elif RN4 == 1:
PS += "1"
elif RN4 == 2:
PS += "2"
elif RN4 == 3:
PS += "3"
elif RN4 == 4:
PS += "4"
elif RN4 == 5:
PS += "5"
elif RN4 == 6:
PS += "6"
elif RN4 == 7:
PS += "7"
elif RN4 == 8:
PS += "8"
elif RN4 == 9:
PS += "9"
elif RN5 == 0:
PS += "0"
elif RN5 == 1:
PS += "1"
elif RN5 == 2:
PS += "2"
elif RN5 == 3:
PS += "3"
elif RN5 == 4:
PS += "4"
elif RN5 == 5:
PS += "5"
elif RN5 == 6:
PS += "6"
elif RN5 == 7:
PS += "7"
elif RN5 == 8:
PS += "8"
elif RN5 == 9:
PS += "9"
elif RN5 == 10:
PS += ""
elif RN6 == 0:
PS += "0"
elif RN6 == 1:
PS += "1"
elif RN6 == 2:
PS += "2"
elif RN6 == 3:
PS += "3"
elif RN6 == 4:
PS += "4"
elif RN6 == 5:
PS += "5"
elif RN6 == 6:
PS += "6"
elif RN6 == 7:
PS += "7"
elif RN6 == 8:
PS += "8"
elif RN6 == 9:
PS += "9"
print(PS)
PG += 1
PS = ""Python版本: 3.7.4
发布于 2019-12-11 00:10:46
让我们一步一步地检查您的代码:
首先,注意random.randint返回一个整数。因此,您需要将其转换为字符串。您可以使用str()函数将其转换为字符串,例如:
str(random.randint(0, 9))+"9"将返回一个类似59的字符串(例如)。
因此,在初始化每个随机数时,您需要通过以下方式进行初始化,例如:
RN1 = str(random.randint(0, 9))然后,不需要检查每个随机变量的值,只需将它们相加:
PS = RN1 + RN2 + RN3 + RN4 + RN5 + RN6此外,您可以对前四个变量使用for循环,而不是使用六个不同的变量来处理随机值,前四个变量的值从0到9:
for x in range(4):
RN = str(random.randint(0, 9))
PS += RN然后将其余两个介于0和10之间的值相加:
PS += str(random.randint(0, 10))
PS += str(random.randint(0, 10))发布于 2019-12-11 00:13:52
import random
# PTG = int(input("Enter the amount of pins to generate: \n"))
PTG = 10
PG = 0
PS = ""
while PTG > PG:
RN1 = random.randint(0, 9)
RN2 = random.randint(0, 9)
RN3 = random.randint(0, 9)
RN4 = random.randint(0, 9)
RN5 = random.randint(0, 10)
RN6 = random.randint(0, 10)
PS = str(RN1) + str(RN2) + str(RN3) + str(RN4) + str(RN5) + str(RN6)
print(int(PS))
PG += 1发布于 2019-12-11 00:19:15
当我正确理解您的代码时,您希望生成6位数的n个引脚。你可以做的比你想做的要容易得多:
number_of_pins = int(input("Enter the amount of pins to generate: \n"))
pins = []
for i in range(number_of_pins):
pins.append(str(random.randint(100_000, 999_999)))
print(" ".join(pins))解释:
pins = []创建一个新的空列表来存储引脚
for i in range(n):执行下面的缩进块n次。
pins.append(random.randint(100_000, 999_999))生成一个随机数并将其添加到列表中。100000是6位数的第一个数字,999999是最后一个数字。(_只是为了可读性)。str()将其转换为字符串。
print(“".join( pins ))连接所有引脚,并在其间放置一个空格。
https://stackoverflow.com/questions/59271283
复制相似问题