Home > other >  combining user input variables into one variable. One of which may not be a variable
combining user input variables into one variable. One of which may not be a variable

Time:01-25

I am making a password generator. How do I make it that if the user enters no to the questions. no uppercase, for example. The generator sill works just without the uppercase?

   print("hello, Welcome to Password Generator")
            length = int(input('Please enter the length of password: '))
            upperanswer = input("Would you like to use UpperCase? ")
            if upperanswer == "yes":
                    upper = string.ascii_uppercase
            loweranswer = input("Would you like to use LowerCase?")
            if loweranswer == "yes":
                    lower = string.ascii_lowercase
            numberanswer = input("Would you like to use Numbers?")
            if numberanswer == "yes":
                    numbers = string.digits
            specialanswer = input("Would you like to use Special Characters?")
            if specialanswer == "yes":
                    special = string.punctuation
            all = lower   upper   numbers   special
            temp = random.sample(all,length)
            password = "".join(temp)
            print(password)

CodePudding user response:

The issue with this code is that if you answer anything but yes to upper the variable will be undefined. I would recommend extending a string which i've called characters, this way you can refer to it no matter what the user answers.

#!/usr/bin/env python3
import string
import random
print("hello, Welcome to Password Generator")

characters = ""

length = int(input('Please enter the length of password: '))
upperanswer = input("Would you like to use UpperCase? ")
if upperanswer == "yes":
        characters  = string.ascii_uppercase
loweranswer = input("Would you like to use LowerCase? ")
if loweranswer == "yes":
        characters  = string.ascii_lowercase
numberanswer = input("Would you like to use Numbers? ")
if numberanswer == "yes":
        characters  = string.digits
specialanswer = input("Would you like to use Special Characters? ")
if specialanswer == "yes":
        characters  = string.punctuation

temp = random.sample(characters, length)
password = "".join(temp)
print(password)

result:

hello, Welcome to Password Generator
Please enter the length of password: 10
Would you like to use UpperCase? no
Would you like to use LowerCase? yes
Would you like to use Numbers? no
Would you like to use Special Characters? yes
%-injo!&^e

CodePudding user response:

Simply set the strings empty when the user enters no, by adding an else statement to each if statement see code below. Note that this will of course fail if the user tries to enter no for all of the options, as then no characters will be available

import random
import string

print("hello, Welcome to Password Generator")
length = int(input('Please enter the length of password: '))
upperanswer = input("Would you like to use UpperCase? ")
if upperanswer == "yes":
    upper = string.ascii_uppercase
else:
    upper = ""
loweranswer = input("Would you like to use LowerCase?")
if loweranswer == "yes":
    lower = string.ascii_lowercase
else:
    lower = ""
numberanswer = input("Would you like to use Numbers?")
if numberanswer == "yes":
    numbers = string.digits
else:
    numbers = ""
specialanswer = input("Would you like to use Special Characters?")
if specialanswer == "yes":
    special = string.punctuation
else:
    specialanswer = ""
all = lower   upper   numbers   special
temp = random.sample(all, length)
password = "".join(temp)
print(password)
  •  Tags:  
  • Related