hewwo

I'm having trouble with the following Python script:

#!/usr/bin/python3

import random
import string

def generate_random_character(seed):
    """Generates a random alphanumeric character.
    """
    random.seed(seed, version=1) # Version parameter should be removed if using Python 2
    code = random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits)

    return code

def generate_random_codes(group_of_seeds):
    """Generates a number of random alphanumeric strings.
    """
    output = ""

    for i in range(len(group_of_seeds)):
        for seed in group_of_seeds[i]:
            output = generate_random_character(seed)

        output += "\n"

    return output

SEEDS_GROUP = ([113,205,84,36,30],
               [48,44,3,13,74],
               [88,17,60,29,47],
               [9,165,94,99,77],
               [58,113,78,248,128],
               [46,60,31,147,17]) 

print (generate_random_codes(SEEDS_GROUP))

I thought it would generate a number of random alphanumeric codes but the only output I get when I run it is a single character:

h

I already posted it on StackOverflow and a bundle of people kept saying it's a "one character fix", but that just seems to be repeating my problem!

Can anybody help? You should be able to execute it online here.

5 years ago

Comment has been collapsed.

My guess would be that output is overwritten every time

5 years ago
Permalink

Comment has been collapsed.

And the one character fix would be to add a + in output +=generate_random....

5 years ago
Permalink

Comment has been collapsed.

Change

output = generate_random_character(seed)

to

output += generate_random_character(seed)

That should do the trick.

5 years ago
Permalink

Comment has been collapsed.

This. You need to add the the list, if you just use a = sign it'll overwrite the value each loop.

5 years ago
Permalink

Comment has been collapsed.

+1.

5 years ago
Permalink

Comment has been collapsed.

Bump!

5 years ago
Permalink

Comment has been collapsed.

Don't know what you're trying to do exactly but using static seeds will always produce the same output. Computers are deterministic systems so the same starting conditions will always lead to the same results.
If that's not what you're trying to do, what you could do instead is generate a GUID then hash it and use it as a seed.

edit: ok guess you wanted fixed results

5 years ago*
Permalink

Comment has been collapsed.

I find it puzzling when people try to make sense of computer nerds.

5 years ago
Permalink

Comment has been collapsed.

View attached image.
5 years ago
Permalink

Comment has been collapsed.

First thing to do when code doesn't work: set a break point and step through line by line.

5 years ago
Permalink

Comment has been collapsed.

Bump! I've seen you already got anwers, but thank you for the link to the online compiler.

5 years ago
Permalink

Comment has been collapsed.

Bump!

5 years ago
Permalink

Comment has been collapsed.

Bump.

5 years ago
Permalink

Comment has been collapsed.

Amazing how one character can screw up your wife... I mean, Life!

5 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 4 years ago.

5 years ago
Permalink

Comment has been collapsed.

View attached image.
5 years ago
Permalink

Comment has been collapsed.

bump

5 years ago
Permalink

Comment has been collapsed.

Nice one :D Bump!
Unrelate image.

View attached image.
5 years ago
Permalink

Comment has been collapsed.

put on hold as off-topic by grego87 ♦ 1 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – grego87 ♦

If this question can be reworded to fit the rules in the help center, please edit the question.

5 years ago
Permalink

Comment has been collapsed.

Hello, old f-friend......

5 years ago
Permalink

Comment has been collapsed.

Closed 5 years ago by Wormy.