Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以字符串格式添加随机值时得到错误结果_Python_Python 3.x_Random - Fatal编程技术网

Python 以字符串格式添加随机值时得到错误结果

Python 以字符串格式添加随机值时得到错误结果,python,python-3.x,random,Python,Python 3.x,Random,我正在尝试制作一个脚本,它接收一些期望的随机数作为输入,然后生成并打印它们 但是,我的脚本添加数字,而不是连接字符串。我想让字符串连接起来,这样它就可以生成PIN,如: 输入要生成的午餐PIN数量: 十, 我的代码: import random PTG = int(input("Enter the amount of pins to generate: \n")) PG = 0 PS = "" while PTG > PG: RN1 = random.randint(0, 9) R

我正在尝试制作一个脚本,它接收一些期望的随机数作为输入,然后生成并打印它们


但是,我的脚本添加数字,而不是连接字符串。我想让字符串连接起来,这样它就可以生成PIN,如:

输入要生成的午餐PIN数量: 十,

我的代码:

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

首先,请注意,
random.randint
返回一个int。因此需要将其转换为字符串。 您可以使用该函数将其转换为字符串,例如:

str(random.randint(0, 9))+"9"
RN1 = str(random.randint(0, 9))
将返回一个字符串,如
59
(例如)

因此,在初始化每个随机数时,您需要按照以下方式进行操作,例如:

str(random.randint(0, 9))+"9"
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))

让我们一步一步地浏览您的代码:

首先,请注意,
random.randint
返回一个int。因此需要将其转换为字符串。 您可以使用该函数将其转换为字符串,例如:

str(random.randint(0, 9))+"9"
RN1 = str(random.randint(0, 9))
将返回一个字符串,如
59
(例如)

因此,在初始化每个随机数时,您需要按照以下方式进行操作,例如:

str(random.randint(0, 9))+"9"
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))

当我正确理解您的代码时,您希望生成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=[]
创建一个新的空列表来存储pins

对于范围(n)中的i:
执行以下缩进块n次

pins.append(random.randint(100000999))
生成一个随机数并将其添加到列表中。100000是第一个6位数的数字,999999是最后一个数字。(该u仅用于可读性)。str()将其转换为字符串


print(“.join(pins))连接所有管脚并在管脚之间留出一个空格。

当我正确理解您的代码时,您希望生成具有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=[]
创建一个新的空列表来存储pins

对于范围(n)中的i:
执行以下缩进块n次

pins.append(random.randint(100000999))
生成一个随机数并将其添加到列表中。100000是第一个6位数的数字,999999是最后一个数字。(该u仅用于可读性)。str()将其转换为字符串


print(“.join(pins))连接所有pin并在它们之间留出一个空格。

“我的脚本添加数字而不是连接字符串”实际上,您的问题更可能是在第一次检查一些变量时出现了
elif
,因此它们永远不会被检查。如果确保所有变量的第一个检查是
if
,则可能就是整个解决方案。(当然,除了改进从整数到字符串的转换方式)现在只有
RN1
RN3
从它们自己的
if
开始,所有其他变量只有在前面的变量没有条件实现时才会被添加,因为它们只有
elif
块。“我的脚本添加数字而不是连接字符串”实际上,您的问题更可能是在第一次检查一些变量时出现了
elif
,因此它们永远不会被检查。如果您确保所有变量的第一次检查都是
if
,那么这可能就是整个解决方案。(当然,除了改进从整数到字符串的转换方式)现在只有
RN1
RN3
从它们自己的
if
开始,所有其他变量只有在前面的变量没有条件实现时才会被添加,因为它们只有
elif
块。