首页 > 解决方案 > 想要从列表中选择一个随机项目并在不同的函数中重复使用该项目

问题描述

我的代码很乱,因为我是新手,但我无法弄清楚!

顺便说一句,我只包含了我的代码的相关部分。

几乎我想要它做的是为 random_place1 说两次相同的位置,然后为 random_place2 说一个新位置。目前它只给了我三个不同的位置,因为我只是要求 random_place1 重新随机化。

def random_place1():

    import random

    locations = ["Paris" , "New York", "San Francisco" , "Tokyo", "Istanbul", "São Paulo", "Xi'an", "Bogotá", "Casablanca", "Rabat"]


    first_place = random.choice(locations)

    return (first_place)

def random_place2():

    import random

    locations = ["London" , "Miami" , "Chicago", "Cairo", "Vienna" , "Manila", "Munich", "Delhi"]


    second_place = random.choice(locations)
    return(second_place)

def main():

    answer = input("Type Yes to continue or No to exit: ")
    if answer == "yes":
        print("\nLast summer, we went for a vacation with " + random_name(), "on a trip to " + random_place1(), ". The weather there is very " + random_adj(), "! Northern " + random_place1(), " has many " + random_plural_noun(), ", and they make " + random_plural_noun(), " there. Many people there also go to the " + random_place2()," to " + random_action_verb(), ". The people who live there like to eat " + random_food(), ". They also like to " + random_action_verb(), " in the sun and swim in the " + random_noun(), ". It was a really " + random_adj(), " trip!")
        restart2()
    elif answer == "y" :
        print("\nLast summer, we went for a vacation with " + random_name(), "on a trip to " + random_place1(), ". The weather there is very " + random_adj(), "! Northern " + random_place1(), " has many " + random_plural_noun(), ", and they make " + random_plural_noun(), " there. Many people there also go to the " + random_place2()," to " + random_action_verb(), ". The people who live there like to eat " + random_food(), ". They also like to " + random_action_verb(), " in the sun and swim in the " + random_noun(), ". It was a really " + random_adj(), " trip!")
        restart2()
    elif answer == "Yes":
        print("\nLast summer, we went for a vacation with " + random_name(), "on a trip to " + random_place1(), ". The weather there is very " + random_adj(), "! Northern " + random_place1(), " has many " + random_plural_noun(), ", and they make " + random_plural_noun(), " there. Many people there also go to the " + random_place2()," to " + random_action_verb(), ". The people who live there like to eat " + random_food(), ". They also like to " + random_action_verb(), " in the sun and swim in the " + random_noun(), ". It was a really " + random_adj(), " trip!")
        restart2()
    elif answer == "YES":
        print("\nLast summer, we went for a vacation with " + random_name(), "on a trip to " + random_place1(), ". The weather there is very " + random_adj(), "! Northern " + random_place1(), " has many " + random_plural_noun(), ", and they make " + random_plural_noun(), " there. Many people there also go to the " + random_place2()," to " + random_action_verb(), ". The people who live there like to eat " + random_food(), ". They also like to " + random_action_verb(), " in the sun and swim in the " + random_noun(), ". It was a really " + random_adj(), " trip!")
        restart2()
    elif answer == "Y":
        print("\nLast summer, we went for a vacation with " + random_name(), "on a trip to " + random_place1(), ". The weather there is very " + random_adj(), "! Northern " + random_place1(), " has many " + random_plural_noun(), ", and they make " + random_plural_noun(), " there. Many people there also go to the " + random_place2()," to " + random_action_verb(), ". The people who live there like to eat " + random_food(), ". They also like to " + random_action_verb(), " in the sun and swim in the " + random_noun(), ". It was a really " + random_adj(), " trip!")
        restart2()

    elif answer == "no":
        print("\nThanks for trying out the Madlibs Generator!")
    elif answer == "n":
        print("\nThanks for trying out the Madlibs Generator!")
    elif answer == "No":
        print("\nThanks for trying out the Madlibs Generator!")
    elif answer == "NO":
        print("\nThanks for trying out the Madlibs Generator!")
    elif answer == "N":
        print("\nThanks for trying out the Madlibs Generator!")
    else:
        print("\nInvalid response please try again!")
        restart()

标签: pythonpython-3.xlistrandompython-requests

解决方案


您可以调用随机放置方法并将其返回的值存储在变量中,以便以后使用该值。您可能想要查看的一种编码原则称为“DRY”(不要重复自己)。如果您看到自己编写了很多相同的代码,那么您可能会采取更好的方法。

因此,下面只是快速更新您的代码以使其更清晰。您真的不必为 func random_place 烦恼,因为我们可以调用 random.choice,而不是从 main 调用它。但是,我将其包含在内是为了向您展示您不需要写 random_place 两次。您可以编写一次并给它一个可供选择的位置。

您也可以将输入设为小写。那么您只需要测试“yes”或“y”之类的东西,您不需要测试所有区分大小写的版本。因此,您只需编写一次打印行。

import random

locations1 = ["Paris", "New York", "San Francisco", "Tokyo", "Istanbul", "São Paulo", "Xi'an", "Bogotá", "Casablanca",
              "Rabat"]
locations2 = ["London", "Miami", "Chicago", "Cairo", "Vienna", "Manila", "Munich", "Delhi"]

def random_place(locations):
    return random.choice(locations)

def main():
    while True:
        answer = input("Type Yes to continue or No to exit: ").lower()
        if answer == "yes" or answer == "y":
            location1 = random_place(locations1)
            location2 = random_place(locations2)
            print(f"Last year i went to {location1}. This year i was going to go again to {location1}.",
                  f"However my friends suggested to go to {location2} as the people in {location2} are nicer")

        elif answer == "no" or answer == "n":
            print("\nThanks for trying out the Madlibs Generator!")
            break

        else:
            print("\nThats not a valid input")


main()

输出

Type Yes to continue or No to exit: yes
Last year i went to São Paulo. This year i was going to go again to São Paulo. However my friends suggested to go to Manila as the people in Manila are nicer
Type Yes to continue or No to exit: nodf

Thats not a valid input
Type Yes to continue or No to exit: no

Thanks for trying out the Madlibs Generator!

推荐阅读