how to make the user choose between options

Question:

I make password genrator using python then the teacher told to create a code that make the user choose what he want in the password
the password contains
1- lower case characters
2- upper case characters
3- numbers
4- punticuation marks

the teacher want me to make the user choose if he want punticution or not
if he dont want it have to be deleted from the password
i tried so hard but i got stuck

from msilib import change_sequence
import string   
import random  # I import this line to  make the password unorgnaized

# this code below is all the characters we need for the password
s1 = list(string.ascii_lowercase)  
s2 = list(string.ascii_uppercase)
s3 = list(string.digits)
s4 = list(string.punctuation)

# this code is for the user to put how much characters he need
characters_number = input("how many characters for the password?:") 




# this while loop code is to make the user choose only 6 characters and up
while True:  
    try:                                                                                          
                                                                                                            
        characters_number = int(characters_number)
        if characters_number < 6 :
            print("you need at least 6 characters") # if the user choose an letter or digits it will not allow him
            characters_number = input("please enter the number again:")
                 
        else:
            break # I break the loop here if the user write the correct data 
          
    except: #this code here if the user enter anything except numbers
        print("please enter numbers only")
        characters_number = input("how many characters for the password?:")

# the random and shuffle is to make the password unorgnized            
random.shuffle(s1)
random.shuffle(s2)
random.shuffle(s3)
random.shuffle(s4)

# the password that will appear to the user it contains upper and lower letters, numbers and digit
# part1 here is for the letters I allocated 30% of the password for letters 
# part2 here is for the digits and numbers I allocated 20% of the password for letters
part1 = round(characters_number * (30/100))
part2 = round(characters_number * (20/100))

password = []

for x in range(part1):
    password.append(s1[x])
    password.append(s2[x])
    #the for loops here here is to create the password
for x in range(part2):
    password.append(s3[x])
    password.append(s4[x])


#this code here is to transform the password for list method to a string
password = "".join(password[0:]) 

print(password)
Asked By: BILAL BSA

||

Answers:

You can write a function like this:

def isUserNeedPunctuationMarks():
    result = None
    while True:
        inp = input("Do you need punctuation marks in your password? (y/n)")
        inp = inp.lower()
        if inp == "y":
            result = True
            break
        elif inp == "n":
            result == False
            break
        else:
            print("You must enter y or n")
    
    return result

The above function will return True if the user needs punctuation marks, and False otherwise. User has to type y (for yes) or n(for no).

You can call isUserNeedPunctuationMarks() in the right place in your code. For example, removing punctuation marks from password string, you have to write a condition like this:

import re

if not isUserNeedPunctuationMarks():
    password = re.sub(r'[^ws]', '', password) # This removes punctuation marks from a string using regex

Hope this helps 🙂

Answered By: Usitha Indeewara
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.