Currently the main code for the game is defined at the module level, outside of any function. I.e., the start of the module looks like this at the moment...
import random
import getpass
import pycountry
from countryinfo import CountryInfo
# Fetch all countries and their capitals using pycountry
countries_capitals = {}
for country in pycountry.countries:
try:
country_info = CountryInfo(country.alpha_2)
capital = country_info.capital()
if capital:
countries_capitals[country.name] = capital
except (KeyError, ValueError):
continue
# Function to choose a random country from the dictionary
def choose_country():
country = random.choice(list(countries_capitals.keys()))
return country
# Function to check if the guess is correct
def check_guess(guess, country):
capital = countries_capitals[country]
if guess.lower() == capital.lower():
return True
else:
return False
# Initialize player scores
player1_score = 0
player2_score = 0
#Defining the congratulatory message
congrats_message ="Congratulations Player {winner}, you are the winner!"
[... rest of the main game code ...]
Putting code at the module level like this, outside of a function, can cause some problems. For example, currently flake8 is not picking up the fact that the congrats_message variable is declared but never used (accessed).
It would be better to move the main logic into a function. Something like this would be conventional...
import random
import getpass
import pycountry
from countryinfo import CountryInfo
# Function to choose a random country from the dictionary
def choose_country():
country = random.choice(list(countries_capitals.keys()))
return country
# Function to check if the guess is correct
def check_guess(guess, country):
capital = countries_capitals[country]
if guess.lower() == capital.lower():
return True
else:
return False
def main():
"""Main game logic."""
# Fetch all countries and their capitals using pycountry
countries_capitals = {}
for country in pycountry.countries:
try:
country_info = CountryInfo(country.alpha_2)
capital = country_info.capital()
if capital:
countries_capitals[country.name] = capital
except (KeyError, ValueError):
continue
# Initialize player scores
player1_score = 0
player2_score = 0
#Defining the congratulatory message
congrats_message ="Congratulations Player {winner}, you are the winner!"
[... rest of the main game code ...]
if __name__ == "__main__":
# Call the main function if script has been run from the command line.
main()
I.e., here all of the main game logic is moved inside a new function called main().
Then, at the end of the script, there is a special code block:
if __name__ == "__main__":
# Call the main function if script has been run from the command line.
main()
...which is not strictly necessary but helps to clarify that you expect this module to be run from the command line.
Currently the main code for the game is defined at the module level, outside of any function. I.e., the start of the module looks like this at the moment...
Putting code at the module level like this, outside of a function, can cause some problems. For example, currently flake8 is not picking up the fact that the
congrats_message
variable is declared but never used (accessed).It would be better to move the main logic into a function. Something like this would be conventional...
I.e., here all of the main game logic is moved inside a new function called
main()
.Then, at the end of the script, there is a special code block:
...which is not strictly necessary but helps to clarify that you expect this module to be run from the command line.