0
import random
import string

newPassword = input("Do you want a new password?: ")
characters = input("How many characters do you want your new password to have?: ")

def passwordGenerator():
    if newPassword.lower() == "yes": 
        print("Your new password is:")  
        for i in range(int(characters)):  
            print(''.join(random.choices(string.ascii_uppercase + string.digits)))  
    elif newPassword.lower() == "no":  
        print("Okay, maybe some other time!")  
    else:  
        print("Invalid input!")

passwordGenerator()

When I run the code the random characters print out on separate lines, something like:

Y

B

3

R

M

How do I join these characters on a same line to appear as a string?

KarlR
  • 1,545
  • 12
  • 28
  • 1
    Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – user3486184 Aug 23 '18 at 00:05

2 Answers2

0

Just try it that way:

import random
import string

newPassword = input("Do you want a new password?: ")
characters = input("How many characters do you want your new password to have?: ")

def passwordGenerator():
    if newPassword.lower() == "yes": 
        password = ''  
        for i in range(int(characters)):  
            password += ''.join(random.choices(string.ascii_uppercase + string.digits))
        print("Your new password is : " + password)  
    elif newPassword.lower() == "no":  
        print("Okay, maybe some other time!")  
    else:  
        print("Invalid input!")

passwordGenerator()

You are adding letters to empty word so after for..in loop You have Your random password.

KarlR
  • 1,545
  • 12
  • 28
0

Simplify the function.

newPassword = input("Do you want a new password?: ")
newPassword = newPassword.lower()
characters = input("How many characters do you want your new password to have?: ")
chars = string.ascii_uppercase + string.digits

def passwordGenerator(length):
    return ''.join(random.choice(chars) for x in range(length))

if newPassword == 'yes':
   print(passwordGenerator(characters))
elif newPassword == 'no':
   print("Okay, maybe some other time!")  
else:  
   print("Invalid input!")
bigbounty
  • 16,526
  • 5
  • 37
  • 65