I am trying to create a "Higher-Lower" game for a Python course I am undertaking. In short, I am supplying the program with a list of Instagram celebrities, each with a description and number of followers. The player sees in succession pairs of celebrities from the list and must indicate which celebrity has more followers. The game continues until the user makes a wrong guess or the list ends.
In the current form, I am importing the list of celebrities from another module. The problem is that whenever the function which runs the game re-initializes (i.e., when the player opts to restart the game when they lose or when the player successfully goes through the entire list), the list misses all the items that have already been shown to the player. So, if the player loses and wants to restart the game, they only get to go through the items not yet shown. Otherwise, if they successfully went through the entire list, if they restart the game an error comes up because the list of celebrities is now empty.
When instead of importing the list from a different module I define the list in the same module and redefine it before each execution of the function which runs the program, everything works as intended. I have been trying so many things to make it work by importing it from a different module, but I just can't seem to work it out - the imported list does not reset to its initial form. Any help would be greatly appreciated!
The full code with the list imported from a different module can be found below. The data used (and included in the separate module 'game_data_test') is included after the code.
#Higher-Lower Game Project
import random
from game_data_test import data
def game():
global data
new_items = data
print(data)
print(new_items)
print("Welcome to the 'Higher-Lower' game!")
round_number = 1
streak = 0
status = "win"
def influencer_selection():
number_influencers = len(new_items)
influencer_index = random.randint(0, number_influencers-1)
selected_influencer = new_items[influencer_index]
new_items.remove(selected_influencer)
return selected_influencer
def influencer_info(influencer_code):
return (f"{influencer_code['name']}, a {influencer_code['description']}, from {influencer_code['country']}.")
influencer_a = influencer_selection()
influencer_a_info = influencer_info(influencer_a)
while status == "win" and len(new_items) > 0:
print(f"Round {round_number}:\n\n")
influencer_b = influencer_selection()
influencer_b_info = influencer_info(influencer_b)
print(f"Compare A: {influencer_a_info}")
print(f"\nAgainst B: {influencer_b_info}\n")
if int(influencer_a['follower_count']) > int(influencer_b['follower_count']):
correct_answer = "A"
else:
correct_answer = "B"
answer = input("Who has a higher number of Instagram followers? A or B? \n\n")
if answer == correct_answer:
print("\nYou're right!")
round_number += 1
streak += 1
print(f"\nYou are at {streak} correct guesses!\n")
influencer_a = influencer_b
influencer_a_info = influencer_b_info
else:
print("\nOh no, you're wrong!")
status = "loss"
print(f"\nYou have lost. You had a streak of {streak} correct guesses. Congratulations!\n")
again = input("Do you want to play again? 'Y' or 'N'\n\n")
if again == "Y":
from game_data_test import data
game()
else:
print("\nThank you for playing!")
if len(new_items) == 0:
print(f"\nCongratulations, you have completed the game with a streak of {streak} correct guesses!\n")
again = input("Do you want to play again? 'Y' or 'N'\n\n")
if again == "Y":
from game_data_test import data
game()
else:
print("\nThank you for playing!")
game()
#Data included in separate module game_data_test
data = [
{
'name': 'Instagram',
'follower_count': 346,
'description': 'Social media platform',
'country': 'United States'
},
{
'name': 'Cristiano Ronaldo',
'follower_count': 215,
'description': 'Footballer',
'country': 'Portugal'
},
{
'name': 'Ariana Grande',
'follower_count': 183,
'description': 'Musician and actress',
'country': 'United States'
},
{
'name': 'Dwayne Johnson',
'follower_count': 181,
'description': 'Actor and professional wrestler',
'country': 'United States'
}]