簡體   English   中英

我正在使用 Python 制作一個簡單的密碼生成器,運行它時沒有任何反應,但我也沒有看到任何錯誤。 我做錯了什么?

[英]I am making a simple password generator using Python and nothing happens when I run it but I also see no errors. What did I do wrong?

我正在嘗試構建一個簡單的密碼生成器,它接受用戶輸入並根據答案將字符列表添加到一個更大的列表中,然后可以隨機生成一個密碼。 我不確定我哪里出錯了。 我知道可能有更有效的方法來做到這一點,但我需要為我的任務使用多個列表和循環。 我沒有收到任何錯誤,但運行它時沒有任何反應。 預先感謝您幫助我。

# import random generator module
import random

# creates three lists: letters, digits, and symbols 
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n','o', 'p', 'q',  'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'O', 'p', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] 
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 
symbols = ['!', '@', '#', '"', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '>', '=', '?', '[', ']', '^', '_', '`', '{', '}', '|', "~"]
password = []

# This gets the desired length for the password from the user
def user_password_length():
    pw_length = input("Hello! How long would you like your password to be?: ")
    return int(pw_length)

#This asks if the user wants to include letters, and adds it to password list if they do

def user_letters_choice():
  include_letters = input("Would you like to include letters in your password? Answer yes or no:  ")
  if include_letters == "yes":
    password.extend(letters)
    return password
  elif include_letters == "no":
    pass
  else:
    print("Make sure your input is lowercase yes or no!") 

#This asks if user wants to include digits in their password and adds it to password list if they do
def user_digits_choice():
  include_digits = input("Would you like to include digits in your password? Answer yes or no: ")
  if include_digits == "yes":
    password.extend(digits)
    return password
  elif include_digits == "no":
    pass
  else:
    print("Make sure your input is lowercase yes or no!") 

# This asks if users want to include symbols in their password and adds it to password list if they do

def user_symbols_choice():
  include_symbols = input("Would you like to include symbols in your password? Answer yes or no: ")
  if include_symbols == "yes":
    password.extend(symbols)
    return password
  elif include_symbols == "no":
    pass
  else:
    print("Make sure your input is lowercase yes or no!") 

# tells script to run x number of times based on user's chosen length for their password and then randomly chooses x amount of characters from the list of available characters in password
def password_generator(pw_length):
  for x in range(pw_length):
    user_pw = random.choice(password)
    print(user_pw)

您已經編寫了方法,現在您需要構建如何使用這些方法。 當您定義一個方法時,它也不會調用該方法。 要調用已創建的方法,只需調用帶括號的方法名稱即可。 例如,這是一個假設的方法定義:

def doSomething(name):
    print("hi %s" % name)

現在要在你的代碼中調用這個函數,你可以這樣調用它:

doSomething("steve")

函數工作,除非他們已經調用。 首先你必須運行函數才能知道然后你有正確的答案。 例如,沒有縮進user_password_length()將在您的代碼中調用 'user_password_length' 函數。 祝你好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM