我在python循环中苦苦挣扎我试图让程序继续重复,直到它输出一个较低的,高位和数字但它在我运行程序时结束
import random
num = 3
print(num)
message=""
for n in range(num):
while message =="":
newnum= random.randint(48,122)
if newnum > 47 and newnum < 58:
val=chr(newnum)
val = newnum
message +=chr(val)
elif newnum > 64 and newnum <91:
val=chr(newnum)
val = newnum
message +=chr(val)
elif newnum > 97 and newnum < 123:
val=chr(newnum)
val = newnum
message +=chr(val)
else:
newnum -= random.randint(7,10)
val=chr(newnum)
val = newnum
message +=chr(val)
if (any(x.isupper() for x in message) and any(x.islower() for x in message)and any(x.isdigit() for x in message)):
print(message)
else:
message =""
答案 0 :(得分:0)
在第一次迭代后,您的消息将是!=&#39;&#39;,因为您将其设置为chr(val)。因此它永远不会再次进入while循环,它只会完成for循环。
答案 1 :(得分:0)
我认为你的问题是错误的。尝试使用
for n in range(num):
while message =="":
newnum= random.randint(48,122)
if newnum > 47 and newnum < 58:
val=chr(newnum)
val = newnum
message +=chr(val)
elif newnum > 64 and newnum <91:
val=chr(newnum)
val = newnum
message +=chr(val)
elif newnum > 97 and newnum < 123:
val=chr(newnum)
val = newnum
message +=chr(val)
else:
newnum -= random.randint(7,10)
val=chr(newnum)
val = newnum
message +=chr(val)
if (any(x.isupper() for x in message) and any(x.islower() for x in message) and any(x.isdigit() for x in message)):
print(message)
else:
message =""
答案 2 :(得分:0)
你的问题主要是身份和时间条件,请看看我的代码。
import random
num = 3
print(num)
message=""
end = False #set a valid condition to end the program
for n in range(num):
# while message =="":
while end == False:
newnum= random.randint(48,122)
if newnum > 47 and newnum < 58:
val=chr(newnum)
val = newnum
message +=chr(val)
elif newnum > 64 and newnum <91:
val=chr(newnum)
val = newnum
message +=chr(val)
elif newnum > 97 and newnum < 123:
val=chr(newnum)
val = newnum
message +=chr(val)
else:
newnum -= random.randint(7,10)
val=chr(newnum)
val = newnum
message +=chr(val)
if (any(x.isupper() for x in message)
and any(x.islower() for x in message)
and any(x.isdigit() for x in message)):
print(message)
end=True
# else:
# message =""