【问题标题】:Want to select a random item from a list and reuse the item in a different function想要从列表中选择一个随机项目并在不同的函数中重复使用该项目
【发布时间】:2020-03-24 09:42:28
【问题描述】:

我的代码很乱,因为我是新手,但我想不通!

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

几乎我想要它做的是为 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()

【问题讨论】:

  • 在您的代码中留下任何其他问题,在您捕获答案后,只需生成并存储随机位置,然后像loc1 = random_place1() 一样打印它们然后在您的文本中使用loc1 而不是random_place1()
  • 当我尝试这个时,它仍然给我两个不同的位置,而不是一个。
  • 这是不可能的。所以如果你这样做loc1 = random_place1(); print(f"{loc1} {loc1}"),你是说你得到了两个不同的位置?
  • 是的。我认为出现的问题是它只是再次调用 random_places() 引入一个新城市。也许用 random.seed() 做点什么?我只是不知道第一次通过后如何清除和重置种子。
  • N不,你在这里做错了什么。看看我之前发布的答案,它是一个完整的工作示例,并显示 location1 没有改变

标签: python python-3.x list random python-requests


【解决方案1】:

您可以调用随机放置方法并将其返回的值存储在变量中,以便以后使用该值。您可能想要查看的一种编码原则称为“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!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2017-12-23
    • 2017-11-10
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多