0

Ive created two functions for a small game that I made to guess country name. Im trying to put them into class called class GuessTheCountry():

The following code has no problem running, but it needs to be inside a class. Since Im new to Python and the code is already relatively large for me, Im not sure how to put them into class.

import random
from country_list import country_list

class GuessTheCountry():
    def __init__(self, country_list):
        self.country_list = country_list


    #def __repr__(self):
        # return how you want your game represented in string form
        # good explanation of __repr__ method: https://stackoverflow.com/a/1984177/13282454

    def get_country(self):
        # you can use your self.country_list value here
        country = random.choice(self.country_list)
        return country.upper()

    def the_game(self):
        # you can use self values from the __init__ method here
        # you can also call functions like self.get_country() here
        country = self.get_country()
        country_letters = set(country)
        used_letter = set()

        attempt = 75

        print('WELCOME TO GUESS THE COUNTRY GAME')
        while len(country_letters) > 0 and attempt > 0:
            print('Attempt remaining: ', attempt, 'and Used Letter(s): ', " ".join(used_letter))

            # If the letter has been guessed (belongs in country and in used_letter), it will be appended to the output list, else _ is appended
            output_list = []
            for letter in country:
                if letter in used_letter:
                    output_list.append(letter)
                else:
                    output_list.append('_')
            print('Country: ', " ".join(output_list))

            user_input = input('Please enter a letter or space: ').upper()
            # condition for valid input
            if user_input.isalpha() == True not in used_letter and len(user_input) == 1 or user_input == ' ':
                used_letter.add(user_input)
                # this if-else statement is used to validate the while-loop condition
                if user_input in country_letters:
                    country_letters.remove(user_input)
                else:
                    attempt = attempt - 1
            # Invalid input handling
            elif len(user_input) > 1:
                print('Enter one character at a time')
            elif user_input.isalpha() == False:
                print('Please enter alphabet')

        if attempt == 0:
            print('No more attempt allowed')
        else:
            print('Succesful: ', country)


if __name__ == "__main__":
    country_list = []  # your country list
    my_game_class = GuessTheCountry(country_list=country_list)
    # bad example of a unit test but this is how you use assert
    assert my_game_class.country_list == country_list

These are also the requirement:

  • at least 1 private and 2 public self attributes
  • at least 1 private and 1 public method that take arguments, return values and are used by your program
  • an init() method that takes at least 1 argument
  • a repr() method
  • unit tests that prove that your class methods work as expected. The tests should evaluate results using assert statements.

It is due in two hours, so I really need help.

1 Answers1

3

without giving the answers away since this is clearly for a class this might help you get started.

you can use a class to keep track of some values that are used across both functions

class GuessTheCountry():
    def __init__(self, country_list):
        self.country_list = country_list
        # you can store additional state values here, there are some others that would be useful to keep here
        # you can also call functions from here to calculate values that you want to keep track of

    def __repr__(self):
        # return how you want your game represented in string form
        # good explanation of __repr__ method: https://stackoverflow.com/a/1984177/13282454

    def get_country(self):
        # you can use your self.country_list value here 
        ...

    def game(self):
        # you can use self values from the __init__ method here
        # you can also call functions like self.get_country() here


if __name__ == "__main__": 
    country_list = []  # your country list
    my_game_class = GuessTheCountry(country_list=country_list)
    # bad example of a unit test but this is how you use assert
    assert my_game_class.country_list == country_list

Yash
  • 161
  • 1
  • 4
  • I tried your suggestion, please refer above. It compiled without error but now it doesnt do anyting. Also the import statement of country_list is now not used at all im not sure why. – Kiddon Key Apr 26 '22 at 01:49
  • 2
    @KiddonKey yes, they told you they were going to give you a hint to start off, not do your homework for you – juanpa.arrivillaga Apr 26 '22 at 01:57
  • @juanpa.arrivillaga its not a howework, its self project which has class as part of requirements - I dont need to do it, but its good if I do. – Kiddon Key Apr 26 '22 at 01:58
  • 1
    @KiddonKey this website isn't for people doing your tasks for you. Please see the [help] and [ask] – juanpa.arrivillaga Apr 26 '22 at 01:59