GithubHelp home page GithubHelp logo

soniabarasa / country-capital-quiz-game Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 2.0 93 KB

This repository is for general learning purposes. Aim is to learn conventional way to write a python code.

Python 100.00%

country-capital-quiz-game's Introduction

country-capital-quiz-game

This is a fun game in which the players take turns to guess capital cities of different countries in the world.

country-capital-quiz-game's People

Contributors

soniabarasa avatar ahernank avatar alimanfoo avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

country-capital-quiz-game's Issues

Hide input so players cannot see each other's answers

Currently the answer provided by each player is visible on the screen. This means that player 2 is at an advantage, because they can see what player 1 answered before they give their answer. So if player 2 didn't know, they could just copy player 1.

The game could be made more fair by hiding the answers that each player provides. Then, only after both players have given their answers, the game could reveal what the correct answer is.

One way to implement this would be to use the getpass() function from the standard library.

Avoid repeated countries in the same playing round

Currently, there is a risk of being asked for the capital of the same country more than once during a round. This is not ideal, as if you know the answer you will be rewarded twice and if you don't you will be disadvantaged!

There are multiple possible solutions here. For example, you could ask the players how many rounds they would like to play before the game starts, then, generate the list of countries you will be using, check if there are any repeated ones, and if yes, re-generate the list. Another alternative is to reflect more on how the choice of country is being made by random.choice( ) and whether we would want to ensure that the countries are being sampled and not replaced in your dictionary when doing so (i.e. sampling without replacement).

Keep score and congratulate the winner

It might be fun if the game kept a record of how many times each player gave a correct answer. Then, when the players have had enough and decide not to play again, the game could print out the final scores, and congratulate the winner.

Fix duplicated turns for player 1 & 2

In the current version of the code, the players turn is duplicated, for example:

    # player 2 goes next
    print(player_turn_str.format(player_num=2))
    player2_guess = input(f"What is the capital of {country}? ")
    # player 2 goes next
    print("Player 2's turn")
    player2_guess = getpass.getpass(prompt=f"What is the capital of {country}? ")

This has probably been introduced while working through conflicts between this PR and the main branch.

Change repository name

I'd suggest to change the name of this GitHub repo to something a little more specific, e.g., "CapitalQuizGame" or something like that. This might help later on when you have lots of repositories in your GitHub user account.

Move code into a main function

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.

Move all tests to an ad-hoc tests/ directory

Currently, the repository has a single test file test_gameofgame.py which includes multiple testing functions. And we have a GitHub action workflow which runs this test.

This works now as all tests are part of a single test file. As the code base in the repository grows, we will expect to have more code that needs testing, therefore it is a good idea to have a dedicated tests/ directory. When doing this, we will also need to update the GitHub action so it tests all files on this directory.

Update text in `README.md` file

Currently the text reads: This is a fun game in which the players take turns to guess capital cities of different countries in the world . main. It would be good to update this to not have the . main for clarity.

I would suggest doing this through a PR (in the same way we have been doing for files with python code - create a branch and edit the file using your local IDE) instead of using the edit feature in the repository website.

Add more countries and capitals

It would be cool if this game included all countries in the world. That way it would be a real challenge, as I'm sure not everyone knows all the capitals of all the countries!

This might be a good opportunity to use a third-party package, rather than try to manually add all the countries and capitals within the source code for the game. E.g., the pycountry package provides a list of all countries in the world, and the countryinfo package has capital cities for all countries.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.