多次运行一个函数,然后返回所有不同的结果?

时间:2019-05-03 12:14:10

标签: python function loops names

这是我的代码:

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

我正在尝试生成十个不同的名称,然后打印所有不同的名称。现在,当我运行该函数时,只会返回一个名称,而不是十个不同的名称。

for x in range (0, 11)

据我所知,这应该执行十次该功能?

这是我现在得到的结果:

name_generator()
'Username: PurpleLeopard13'

但是我想得到:

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

谢谢!

3 个答案:

答案 0 :(得分:1)

问题是当前您已将return语句放入for循环中。因此,一旦生成名字,就可以返回并离开该函数。

您需要进行一些修改:将函数调用移至for循环内。另外,请遵循以下评论中@Jon Clements的建议。

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

答案 1 :(得分:0)

尝试以下代码。而不是返回for循环,请尝试使用print。您的回报阻止了循环的进一步深入。

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()

答案 2 :(得分:0)

作为对生成器使用的附加说明,使您的生成器能够提供任意个名称。用户可以决定检索多少个名称。

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))