mardi 24 mars 2020

Want to select a random item from a list and reuse the item in a different function

my code is pretty messy as I am new but I cant figure this out!

I have only included the relevant pieces of my code btw.

Pretty much What i want it to do is say the same location twice for random_place1 and then a new location for random_place2. Currently it just gives me three separate locations, because i am just calling for random_place1 to rerandomize.

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



Aucun commentaire:

Enregistrer un commentaire