简体   繁体   中英

Assign Randomly Generated String To Variable

I am trying to create a password generator that consist of 3 parts. I am able to generate a randomized string (tYVb for example) however when I try to store it as a variable 'mod' and attempt to print it, the value had 'None' attached to the end of it (tYVbNone). How do I ONLY store the string to my variable so I can further manipulate it?

from random import *

number = 4;

def myFunc(length):
    while length > 0:
        rndNumber = randint(0, 51)

    print('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'[rndNumber], end='')
        length -= 1

mod = str(myFunc(number))
print (mod)

This is how I would do it (based on what you have provided so far):

from random import *

number = 4;

def myFunc(length):
    value = ''
    while length > 0:
        rndNumber = randint(0, 51)
        value += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'[rndNumber]
        length -= 1
    return value

mod = myFunc(number)
print (mod)

There are several errors OR omissions in the code as presented. I offer some comments in the code below to highlight potential improvements to the code OR to highlight some additions.

from random import *

# you don't need a semi-colon after the 4
number = 4

def myFunc(length):

    # Since we are building a password, we want to start with an empty 
    #     password before the while loop.
    password = ''
    while length > 0:
        rndNumber = randint(0, 51)

        # This step builds a new password incrementally. As you go
        #     through the while loop, it adds a new character to the
        #     end of the password variable.
        password += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'[rndNumber]

        length -= 1
    return password

# you don't need to convert the output to a string using the code above
#     since the process builds a string.    
mod = myFunc(number)
print(mod)

As mentioned by Charles Duffy, random.choice() is a better solution and makes the code more Pythonic:

random.choice()

This function randomly picks from a sequence of items (and under the hood, strings are considered sequences).

This snippet could be inserted in the above code to help clean it up.

letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
letter = random.choice(letters)
password += letter

Your function doesn't return anything and putting a number into it serves no purpose unless one of the variables is global. I'm not sure if you can print on the same line as you define something. Running the code doesn't seem to return anything. Try adding a return statement.

I think the cleanest way to write the code you are looking for would be to do something like this:

import random
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

def myFunc(length):
    return ''.join([random.choice(letters) for x in range(0,length)])

then you can call the function like so:

x = myFunc(10)
print(x)

producing an output that looks like: zWAGjUyhEZ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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