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.