我正在尝试创建一个循环,以便当 y 小于 x 时它将循环遍历 print 语句,同时交替通过它们
I am trying to create a loop so that while y is smaller than x it will loop through print statements while alternating through them
my code and the error
我正在尝试制作一个可以创建随机密码的程序。除此之外,它应该在
之间交替
print(chr(letter), end="")
print(chr(number), end="")
因为它使密码看起来像“A2h8l”,它在数字和字母之间交替。
import random
number = random.randint(48,57)
letter = random.randint(65,122)
print(input("How many characters do you want in your password?"))
x = input
y = int(letter + number)
while int(x > y):
print(chr(letter), end="")
print(chr(number), end="")
然而,出于某种原因,
while int(x > y):
出现错误,我不知道该怎么办。不知道我在做什么或做错了什么,感谢您的帮助。
也许你可以这样做
import random
number = random.randint(48,57)
letter = random.randint(65,122)
x = input("How many characters do you want in your password?")
x = int(x)
y = letter + number
while x > y:
print(chr(letter), end="")
print(chr(number), end="")
您 运行 陷入该特定错误的原因是您将 x 设置为输入函数本身。这样的事情会更接近你想要的:
x = int(input("How many characters do you want in your password?"))
int(1 > 2)
语法不正确。
您不能将这样的比较传递给 int()
函数。正确的做法是
if int(1) > int(2):
尽管如此,如果您传递的是整数开头,您仍然不需要 int()
。
即使解决了这个问题,您的代码仍然存在问题。
根据 x
和 y
的值,您的循环将无限期地 运行 或根本不打印,并且还将不断打印相同的两个字符。
处理此问题的更好方法是:
- 定义密码长度
- 循环
range(length)
- 生成随机整数并将它们附加到字符串
import random
length = int(input('Password Length'))
password = ''
for i in range(length):
password = password + chr(random.randint(65,122)) + chr(random.randint(48,57))
password
#'Q5I4D8p8i9l1p7j0I6l9'
像这样:(你的代码出错只是因为x=input
,所以x是一个不能与int类型比较的函数)
import random
x = int(input("How many characters do you want in your password? ").strip())
count = 0
while x > count:
number = random.randint(ord("0"), ord("9"))
upper = random.randint(ord("A"), ord("Z"))
lower = random.randint(ord("a"), ord("z"))
y = random.choice([number, upper, lower])
print(chr(y), end="")
count += 1
print()
或更多 pythonic
import random
import string
CHARS = string.digits + string.ascii_letters
x = int(input("How many characters do you want in your password? ").strip())
print(''.join(random.choice(CHARS) for _ in range(x)))
my code and the error
我正在尝试制作一个可以创建随机密码的程序。除此之外,它应该在
之间交替print(chr(letter), end="")
print(chr(number), end="")
因为它使密码看起来像“A2h8l”,它在数字和字母之间交替。
import random
number = random.randint(48,57)
letter = random.randint(65,122)
print(input("How many characters do you want in your password?"))
x = input
y = int(letter + number)
while int(x > y):
print(chr(letter), end="")
print(chr(number), end="")
然而,出于某种原因,
while int(x > y):
出现错误,我不知道该怎么办。不知道我在做什么或做错了什么,感谢您的帮助。
也许你可以这样做
import random
number = random.randint(48,57)
letter = random.randint(65,122)
x = input("How many characters do you want in your password?")
x = int(x)
y = letter + number
while x > y:
print(chr(letter), end="")
print(chr(number), end="")
您 运行 陷入该特定错误的原因是您将 x 设置为输入函数本身。这样的事情会更接近你想要的:
x = int(input("How many characters do you want in your password?"))
int(1 > 2)
语法不正确。
您不能将这样的比较传递给 int()
函数。正确的做法是
if int(1) > int(2):
尽管如此,如果您传递的是整数开头,您仍然不需要 int()
。
即使解决了这个问题,您的代码仍然存在问题。
根据 x
和 y
的值,您的循环将无限期地 运行 或根本不打印,并且还将不断打印相同的两个字符。
处理此问题的更好方法是:
- 定义密码长度
- 循环
range(length)
- 生成随机整数并将它们附加到字符串
import random
length = int(input('Password Length'))
password = ''
for i in range(length):
password = password + chr(random.randint(65,122)) + chr(random.randint(48,57))
password
#'Q5I4D8p8i9l1p7j0I6l9'
像这样:(你的代码出错只是因为x=input
,所以x是一个不能与int类型比较的函数)
import random
x = int(input("How many characters do you want in your password? ").strip())
count = 0
while x > count:
number = random.randint(ord("0"), ord("9"))
upper = random.randint(ord("A"), ord("Z"))
lower = random.randint(ord("a"), ord("z"))
y = random.choice([number, upper, lower])
print(chr(y), end="")
count += 1
print()
或更多 pythonic
import random
import string
CHARS = string.digits + string.ascii_letters
x = int(input("How many characters do you want in your password? ").strip())
print(''.join(random.choice(CHARS) for _ in range(x)))