Home > database >  Adding strings together adds brackets and quotation marks
Adding strings together adds brackets and quotation marks

Time:03-17

I'm new to programming and trying to learn it by doing small projects. Currently I'm working on a random string generator and I have it 99% done, but I cant get the output to be the way I want it to be.

First, here is the code:

import random

def pwgenerator():
    print("This is a randomm password generator.")
    print("Enter the lenght of your password and press enter to generate a password.")

    lenght = int(input())
    template = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!" # this is the sample used for choosing random characters
    generator = ""
    for x in range(lenght): # join fcuntion goes trough str chronologically and I want it fully randomized, so I made this loop
        add_on = str(random.sample(template, 1))
        generator = generator   add_on
        #print(add_on) added this and next one to test if  these are already like list or still strings.
        #print(generator)
    print(generator) # I wanted this to work,  but...
    for x in range(lenght): #...created this,  because I thought that  I created list with "generator" and tried to print out a normal string with this
        print(generator[x], end="")

pwgenerator()

The original code was supposed to be this:

 import random
    
def pwgenerator():
    print("This is a randomm password generator.")
    print("Enter the lenght of your password and press enter to generate a password.")

    lenght = int(input())
    template = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!"
    generator = ""
    for x in range(lenght):
        generator = generator   str(random.sample(template, 1))
    print(generator)

pwgenerator()

The problem is that with this original code and an for example an input of 10 I get this result:

['e']['3']['i']['E']['L']['I']['3']['r']['l']['2']

what I would want as an output here would be "e3iELI3rl2"

As you can see in the first code i tried a few things, because it looked to me like i was somehow creating a List with lists as items, that each have 1 entry. So i though I would just print out each item, but the result was (for a user input/lenght of 10):

['e']['3']

So it just printed out each character in that list as a string (inlcuding the brackets and quotation marks) , which I interpret as whatever I created not being a list. but actually still a string

Doing some research - and assuming I still created a string - i found this from W3Schools. If I understand it correctly though Im, doing everything right trying to add strings together.

Can you please tell me whats going on here, specifically why I get the output i get that looks like a list of lists? And if you can spare some more time Id also like to hear for a better way to do this, but I mainly want to understand whats going on, rather than be given a solution. Id like to find a solution myself. :D

Cheers

PS: Just in case you are wondering: Im trying to learn by doing and currently follow the suggested mini projects from HERE. But in this case I read on W3Schools, that the "join" method results in chronological results so I added the additional complication of making it really random.

CodePudding user response:

Okay, so the problem is that random.choice returns list of strings instead of a string as you may see below:

template = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!"
random.sample(template, 1)
Out[5]: ['H']

What actually happened there it was adding strings containing result of list casting (e.g ['H'] was converted to "['H']" and then printed on the screen as ['H']).

After modyfying the function to random.choice it worked fine:

random.choice(template)
Out[6]: 'j'

Switch this random.sample to random.choice in your function and it shall be as you expected.

CodePudding user response:

The random.sample() function returns a list chosen from the given string. That's why you're getting a bunch lists stacked together.

  • Related