Home > Software engineering >  Why are the names that I am randomly generating the same in every iteration of my for loop?
Why are the names that I am randomly generating the same in every iteration of my for loop?

Time:05-24

I wrote some code to generate a simple character(like a story character, not a char), my problem is that I want my program to generate multiple characters, but upon running my code, it simply prints out the same name multiple times.

I have to files, the one where the character class is and the one where I run the code.

I believe the code that is giving my issues is this following code:

``` 
from charClasses import Character
from charClasses import *

male_names = ["Xi", "Gonduang", "Lei", "Bei", "Guan"]
female_names = ["Song", "Wen", "Meihui", "Qiang", "Ling"]
surnames = ["Jin", "Ling", "Ma", "Yeun", "Kim"]
genders = ["Male", "Female"]

for t in range(3):
    randGender = random.choice(genders)

if randGender == "Male":
    randName = random.choice(male_names)
else:
    randName = random.choice(female_names)
randSurname = random.choice(surnames)
randAge = random.randrange(0, 100)

characters = [Character(randName, randSurname, randAge, randGender) for x in range(3)]

for character in characters:
character.print_char_info()

```

Here is my code:

https://pastebin.com/suTampyw

https://pastebin.com/7DTk9JSq

I think I know what the problem is, I think I'm placing the random-generation code at the wrong place, but I just can't seem to wrap my head around how to fix it.

How would I go about fixing my code?

CodePudding user response:

The problem is the line number 19 in the second file where you create the characters array.

characters = [Character(randName, randSurname, randAge, randGender) for x in range(3)]

what this statement is essentially doing is creating 3 Character Objects with the values stores in randName, randSurname, randAge and randGender. These values dont change until the next iteration of the for loop you defined on line 9.

You should be creating a empty character list before the iteration and appending every Character to it, because as your code stands right now, it is creating a character list every iteration of the for loop.

characters = []
for t in range(3):
    randGender = random.choice(genders)
 
    if randGender == "Male":
        randName = random.choice(male_names)
    else:
        randName = random.choice(female_names)
    randSurname = random.choice(surnames)
    randAge = random.randrange(0, 100)
 
    characters.append(Character(randName, randSurname, randAge, randGender))

CodePudding user response:

You're using only the latest randomly generated data to create your characters list. You should instead append to the list at every iteration. Try the below:

characters = list()
for t in range(3):
    randGender = random.choice(genders)
 
    if randGender == "Male":
        randName = random.choice(male_names)
    else:
        randName = random.choice(female_names)
    randSurname = random.choice(surnames)
    randAge = random.randrange(0, 100)
 
    characters.append(Character(randName, randSurname, randAge, randGender))
  • Related