【发布时间】: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