简体   繁体   中英

Run a function multiple times and then return all the different results?

Here is my code:

import random
def name_generator():
    for x in range (0, 11):
        color = ["Red", "Green", "Blue", "White", "Black", "Yellow", "Purple", "Orange", "Pink"]
        animal = ["Cat", "Dog", "Snake", "Mouse", "Tiger", "Leopard", "Moose", "Wolf", "Bear"]
        number = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]

        randomColor = random.randrange(0, len(color))
        randomAnimal = random.randrange(0, len(animal))
        randomNumber = random.randrange(0, len(number))

        name = "Username: " + color[randomColor] + animal[randomAnimal] + number[randomNumber]
        return name

I'm trying to generate ten different names and then print all the different names. Now when I'm running the function I'm only getting one name returned instead of ten different names.

for x in range (0, 11)

This should execute the function ten times as far as I understand?

This is the result I'm getting now:

name_generator()
'Username: PurpleLeopard13'

But I'm trying to get:

name_generator()
'Username: PurpleLeopard13'
'Username: GreenDog10'
'Username: PurpleCat1'...(10 times)

Thanks!

The problem is that currently you have put the return statement inside the for loop. So as soon as you generate the first name, you return and leave the function.

You need a slight modification: Move the function call inside the for loop. Alternatively, follow the suggestion of @Jon Clements in the comments below.

import random
def name_generator():
        color = ["Red", "Green", "Blue", "White", "Black", "Yellow", "Purple", "Orange", "Pink"]
        animal = ["Cat", "Dog", "Snake", "Mouse", "Tiger", "Leopard", "Moose", "Wolf", "Bear"]
        number = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]

        randomColor = random.randrange(0, len(color))
        randomAnimal = random.randrange(0, len(animal))
        randomNumber = random.randrange(0, len(number))

        name = "Username: " + color[randomColor] + animal[randomAnimal] + number[randomNumber]
        return name

for x in range (0, 11):    
    print (name_generator())

# Username: PinkMouse3
# Username: GreenCat8
# Username: GreenMoose2
# Username: BlackTiger14
# Username: BlackSnake11
# Username: PurpleMoose13
# Username: GreenMouse2
# Username: RedBear5
# Username: BlueBear2
# Username: WhiteDog2
# Username: WhiteTiger3

Try the below code. Instead of return in for loop try to use print. Your return prevents the loop to interate further.

import random
def name_generator():
    for x in range (0, 11):
        color = ["Red", "Green", "Blue", "White", "Black", "Yellow", "Purple", "Orange", "Pink"]
        animal = ["Cat", "Dog", "Snake", "Mouse", "Tiger", "Leopard", "Moose", "Wolf", "Bear"]
        number = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]

        randomColor = random.randrange(0, len(color))
        randomAnimal = random.randrange(0, len(animal))
        randomNumber = random.randrange(0, len(number))

        name = "Username: " + color[randomColor] + animal[randomAnimal] + number[randomNumber]

        print(name)

name_generator()

As an additional riff on the use of a generator, make your generator capable of providing an arbitrary number of names. The user can decide how many names too retrieve.

from random import choice
from itertools import islice

def name_generator():
    colors = ["Red", "Green", "Blue", "White", "Black", "Yellow", "Purple", "Orange", "Pink"]
    animals = ["Cat", "Dog", "Snake", "Mouse", "Tiger", "Leopard", "Moose", "Wolf", "Bear"]
    numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]

    while True:
        yield "Username: {}{}{}".format(
           choice(colors),
           choice(animals),
           choice(numbers)
        )


names = list(islice(name_generator(), 10))

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