我正在尝试创建密码生成器。我已经成功创建了一个功能代码,但它不断显示如何生成密码,而不是只显示如下所示的三行:
这是我的代码:
def Generate(): #menu function, Generates a password
global password
print("Generating...")
chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" #These are the avaliable characters available to the password
length = input("password length?") #asks for how long the user wants their password
if int(length) < 25: #Doesn't allow the code to print over 26 characters
length = int(length)
for p in range(3):
password = " "
for c in range(length): #for loop which
password += random.choice(chars) #randomly choses characters at random to create the password
print(password)
else:
print ("The password can only be between 8-24 characterss long")
Generate ()
这是我运行的程序:
Generating...
password length?14
*
*C
*CD
*CDb
*CDbF
*CDbFA
*CDbFAi
*CDbFAiK
*CDbFAiKk
*CDbFAiKkA
*CDbFAiKkAa
*CDbFAiKkAa9
*CDbFAiKkAa9m
*CDbFAiKkAa9mr
7
7Y
7Ys
7Ysy
7Ysyj
7Ysyjj
7Ysyjj2
7Ysyjj28
7Ysyjj28C
7Ysyjj28Ch
7Ysyjj28Chq
7Ysyjj28Chqk
7Ysyjj28Chqk(
7Ysyjj28Chqk(k
E
E%
E%C
E%C8
E%C8(
E%C8(w
E%C8(w7
E%C8(w7M
E%C8(w7Mj
E%C8(w7MjP
E%C8(w7MjPO
E%C8(w7MjPOz
E%C8(w7MjPOzx
E%C8(w7MjPOzxx
你能帮我把代码输出三个字吗?
非常感谢。
答案 0 :(得分:1)
您错误地缩进了这个:
for p in range(3):
password = " "
for c in range(length):#for loop which
password += random.choice(chars)#randomly choses characters at random to create the password
print(password)
应该是这样的:
for p in range(3):
password = " "
for c in range(length):#for loop which
password += random.choice(chars)#randomly choses characters at random to create the password
print(password)
通过将print语句放在第一个循环中,它只会在p的每次迭代结束时执行,而不是每次迭代c都执行。
答案 1 :(得分:1)
我们可以帮助你的事情远远超过你实际要求的东西!我会根据您的要求编写相同的代码(假设我没问题)并附上评论。
import random
def generate_password(): #menu function, Generates a password
# global password # NOT NEEDED BUT I"M ASSUMING THINGS HERE
print("Generating...")
chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"#These are the avaliable characters available to the password
length = int(input("password length?")) #asks for how long the user wants their password
if length <= 26: #Doesn't allow the code to print over 26 characters -> THIS SHOULD BE LESS THAN OR EQUAL TO 26 BC YOU SAID OVER 26 CHARS
for p in range(3):
password = " " # EMPTY STRING.. PYTHON WOULDN'T MIND
for c in range(length): #for loop which
password += random.choice(chars) #randomly choses characters at random to create the password -> CONCATENATION HERE.. WE LOVE IT.
print(password) # THIS WAS OUT OF PLACE.
else:
print ("The password can only be between 8-24 characterss long")
generate_password () # PERFECT!
generate_password()
现在,在我带来一些好的代码之后我就无法在晚上睡觉了,我知道如果我按照自己的方式行事,我可以提高它。
import random
import string
def generate_password(): #menu function, Generates a password
print("Generating...")
try:
length = int(input("password length?")) #asks for how long the user wants their password
if 8 <= length < 25: # Doesn't allow the code to print over 26 characters -> THIS SHOULD BE LESS THAN OR EQUAL TO 26 BC YOU SAID OVER 26 CHARS
password = ''.join(random.choice(string.ascii_letters + string.digits + "!@#$%^&*()?") for _ in range(length)) # throw away variable caution here!
print(password)
else:
print("The password can only be between 8-24 characterss long")
generate_password() # PERFECT!
except Exception as e: # Do some homework on what kind of exception(s) you would like to catch ( like ValueError)
print(e)
pass # Don't just pass it. Do something here depending on the Exception
generate_password()
答案 2 :(得分:0)
你的内部循环中用于字符选择的print语句是这里的罪魁祸首,只需向左推一个缩进它就可以了。
for c in range(length): #for loop which
password += random.choice(chars) #randomly choses characters at random to create the password
print(password)
将其保留在for p in range(3):
循环中。
答案 3 :(得分:0)
当然,只需将打印件移出循环:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.komamitsu.android_ocrsample.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/choose_from_gallery"
android:id="@+id/choose_from_gallery"
tools:context=".MainActivity"
android:layout_marginTop="23dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/take_a_photo"
android:id="@+id/take_a_photo"
tools:context=".MainActivity"
android:layout_marginTop="11dp"
android:layout_below="@+id/choose_from_gallery"
android:layout_centerHorizontal="true" />
<TextView
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/detected_text"
android:layout_alignParentBottom="true"
android:layout_below="@+id/take_a_photo"
android:layout_margin="25dp"
android:layout_centerHorizontal="true"
android:background="#EEEEEE"
android:scrollbars="vertical" />
</RelativeLayout>
你也可以考虑这个:
for c in range(length):
password += random.choice(chars)
print(password)