简体   繁体   English

将字符串加在一起会添加括号和引号

[英]Adding strings together adds brackets and quotation marks

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.目前我正在研究一个随机字符串生成器,我已经完成了 99%,但我无法让 output 成为我想要的样子。

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:问题是使用这个原始代码和例如输入 10 我得到这个结果:

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

what I would want as an output here would be "e3iELI3rl2"我在这里想要的 output 是“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.正如您在第一个代码中看到的那样,我尝试了一些事情,因为在我看来我正在以某种方式创建一个列表,其中列表作为项目,每个项目都有 1 个条目。 So i though I would just print out each item, but the result was (for a user input/lenght of 10):所以我虽然我会打印出每个项目,但结果是(对于用户输入/长度为 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.做一些研究——假设我仍然创建了一个字符串——我从 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?你能告诉我这里发生了什么吗,特别是为什么我得到 output 我得到它看起来像一个列表列表? 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 :D

Cheers干杯

PS: Just in case you are wondering: Im trying to learn by doing and currently follow the suggested mini projects from HERE . PS:以防万一你想知道:我正在尝试边做边学,目前正在关注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.但在这种情况下,我在 W3Schools 上读到,“加入”方法会产生按时间顺序排列的结果,因此我添加了使其真正随机的额外复杂性。

Okay, so the problem is that random.choice returns list of strings instead of a string as you may see below:好的,所以问题是 random.choice 返回字符串列表而不是字符串,如下所示:

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

What actually happened there it was adding strings containing result of list casting (eg ['H'] was converted to "['H']" and then printed on the screen as ['H']).那里实际发生的事情是添加包含列表转换结果的字符串(例如,['H'] 被转换为“['H']”,然后在屏幕上打印为 ['H'])。

After modyfying the function to random.choice it worked fine:在将 function 修改为 random.choice 后,它工作正常:

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

Switch this random.sample to random.choice in your function and it shall be as you expected.在您的 function 中将此 random.sample 切换为 random.choice,它应该如您所料。

The random.sample() function returns a list chosen from the given string. random.sample() function 返回从给定字符串中选择的列表。 That's why you're getting a bunch lists stacked together.这就是为什么您会得到一堆堆叠在一起的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM