Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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_String_Random - Fatal编程技术网

Python 无法打印具有随机字母数字字符串调用的变量

Python 无法打印具有随机字母数字字符串调用的变量,python,python-3.x,string,random,Python,Python 3.x,String,Random,我想用下面函数中的随机字符串分配变量A和B。因为我想在程序的其他地方使用它,所以它可以在自动化的同时输入密码和确认密码。当我运行打印A或打印B时,它不打印任何内容。如何打印生成的内容 import random import string # get random string password with letters, digits, and symbols def get_random_password_string(length): password_characters =

我想用下面函数中的随机字符串分配变量A和B。因为我想在程序的其他地方使用它,所以它可以在自动化的同时输入密码和确认密码。当我运行打印A或打印B时,它不打印任何内容。如何打印生成的内容

import random
import string

# get random string password with letters, digits, and symbols
def get_random_password_string(length):
    password_characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(password_characters) for i in range(length))
    print("Random string password is:", password)

A=get_random_password_string(10)
B=get_random_password_string(10)
print(A)
print(B)

您必须使用
return
关键字。通过使用它,您可以将函数的输出分配给变量,然后打印它

def get_random_password_string(length):
    password_characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(password_characters) for i in range(length))
    return "Random string password is:", password

A=get_random_password_string(10)
B=get_random_password_string(10)
print(A)
print(B)

get\u random\u password\u string()没有返回任何内容。您的函数应该
返回密码
。感谢@Aleksander的寻址,我添加了return(password)而不是return“random string password is:”,password。现在,它正在打印A&B的值。再次感谢您。