I'm guessing I need to use a while loop but I can't figure out where to put it so that it re-asks the question the user mistyped.I've tried putting while before and after the define statement but nothing happens when I input a non-valid answer.
import random
import string
def random_pass(length):
spec_char = input("Would you like special characters in your password? ")
if spec_char == "yes":
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
print("Your password is: ", password)
elif spec_char == "no":
characters = ''.join(random.choice(string.ascii_letters) for i in range(length))
print("Your password is: ", characters)
else:
print("Please only type Yes or No")
char_length = int(input("How many characters would you like your password to be? "))
random_pass(char_length)
Just modified your code a bit
import random
import string
def random_pass(length):
while True:
spec_char = input("Would you like special characters in your password? ")
if spec_char == "Yes":
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
print("Your password is: ", password)
return
elif spec_char == "No":
characters = ''.join(random.choice(string.ascii_letters) for i in range(length))
print("Your password is: ", characters)
return
else:
print("Please only type Yes or No")
char_length = int(input("How many characters would you like your password to be? "))
random_pass(char_length)