mardi 31 janvier 2023

Attempting to generate a random item from a list, use that item in the code, then generate a new item that is distinct from the first, repeate

I am making my first program, a "Quiz" that will generate a random street from a list of streets. Then, give you the option to view the street on google maps in a new window so the user can test their knowledge of the streets location. Then, ask if you want to see another street.
If you answer "y", it will go back to the beginning and generate another random street repeating the process.

I can make this whole thing loop through just fine, except it lists the first street result again the second time through the loop. I wish to have a new distinct random street every time the loop is repeated.

I can fix this problem by not assigning the random.choice function to a variable, however when I do this, the google maps opens up in a new window with a distinct street that is different from the one generated. (I use the variable answer_street to allow the printed street and google maps to be the same variable.)

I am stuck. Thank you in advance for your advice as I am just starting out here and look forward to learning this.

import random
import webbrowser

street_list = ["street1", "street2", "street3", "street4"]

#store the randomly generated street to be used later in the code by assigning a variable
answer_street = random.choice(street_list)

#create a function to display the random street name
def random_street():
    print(answer_street)
    
#Define strURL as a variable, the value is the link for the google maps of the street that was previously generated
strURL = "https://www.google.com/maps/place/" + answer_street + ",+City,+State"

#Define a function to ask user if they want to see the street in google maps
#Then if yes, open google maps
def view_map():
    user_action = input("Do you want to see the street in Google Maps? (Y/N):\n")
    possible_actions = ["y", "n"]
    if user_action == "y":
        webbrowser.open(strURL, new=2)

def play_again():
    see_another = input("Would you like to see another random street? (Y/N):\n")
    possible_actions = ["y", "n"]
    if see_another == "y":
        random_street()
        view_map()
        play_again()
    else:
        print("Stay classy!")
        
        
random_street()  

view_map()

play_again()



Aucun commentaire:

Enregistrer un commentaire