# Calling randint() Mid Loop?

I’m working my way through Python and the Magic 8-Ball exercise. I wanted to use a second randint() function to extend the dialogue (if the 8 ball gives a bad answer then the person shakes it again and gets another answer).

I used a second variable random_number2 = random.randint(1,9) but then had to redefine all the Magic 8-Ball answers. Is there a way to call the generator again without using random_number2 and writing all the answers again?

Precisely the reason lists and dictionaries were born. We can store all the answers in a list and then access one by random index, repeatedly if we wish.

1 Like

Hello Mr. mtf,

I am a python beginner and I am in the Data Analyst Path. We are so lucky to have someone like you around. I have learned a lot with your advise and codes.

Currently, I am in the Frida Kahlo project. I almost done. However, I have been having problems creating a function to add items to the Master List (Which has been zipped as indicated in the project).

The problem that I have been having is not creating the function itself but creating a condition where if the audio number and the name of the paint are already in the master list not too add them to the list. Please, could you please give me some advise on this. Below is my code.

Thank you.

paintings = [“The Two Fridas”, “My Dress Hangs Here”, “Tree of Hope”, “Self Portrait With Monkeys”]
dates = [1939, 1933, 1946, 1940]
paintings = list(zip(paintings, dates))
print(paintings)
paintings.append((“The Broken Column”, 1944))
paintings.append(("'The Wounded Deer", 1946))
paintings.append((“Me and My Doll”, 1937))
print(f"\n{paintings}")
#print(len(paintings))
audio_tour_numbers = list(range(1, 8))
print(audio_tour_numbers)
master_list = list(zip(audio_tour_numbers, paintings))
print(master_list)
master_list_final = master_list
if not number in master_list_final and not paint in master_list_final:
master_list_final.append((number, paint, date))
return master_list_final
else:
return f"Item is in list"

This does not create a copy. It only shares the reference to the same object.

Good morning Mr. mtf,
I took the master_list_final = master_list and reference the master_list directly in the condition and stills append items that are already in the list.
How would you approach it?

Thank you sir.

aintings = [“The Two Fridas”, “My Dress Hangs Here”, “Tree of Hope”, “Self Portrait With Monkeys”]
dates = [1939, 1933, 1946, 1940]
paintings = list(zip(paintings, dates))
#print(paintings)
paintings.append((“The Broken Column”, 1944))
paintings.append(("'The Wounded Deer", 1946))
paintings.append((“Me and My Doll”, 1937))
#print(f"\n{paintings}")
#print(len(paintings))
audio_tour_numbers = list(range(1, 8))
print(audio_tour_numbers)
master_list = list(zip(audio_tour_numbers, paintings))
print(master_list)
if not number in master_list and not paint in master_list:
master_list.append((number, paint, date))
return master_list
else:
return f"Item is in list"
print(master_list)

output:
[(1, (‘The Two Fridas’, 1939)), (2, (‘My Dress Hangs Here’, 1933)), (3, (‘Tree of Hope’, 1946)), (4, (‘Self Portrait With Monkeys’, 1940)), (5, (‘The Broken Column’, 1944)), (6, ("'The Wounded Deer", 1946)), (7, (‘Me and My Doll’, 1937)), (7, ‘Me and My Doll’, 1937)]

Yes, they do reference the exact same list. Change made by either reference will affect the list. Given this is a shallow list, we can make an independent shallow copy using either list slice or list.copy()…

``````separate_list = master_list[:]
``````

Not sure including the number is such a good idea. Build the complete list, then enumerate it afterward, perhaps?

Hello Sir,

I did exactly what you told me creating the audio_number after zipping the two other list and it worked. Now when I try to add a repeated paint, the code doesn’t added.

Thank you so much.

1 Like