簡體   English   中英

將函數放入類中 Python

[英]Putting Functions into Classes in Python

我為一個猜國名的小游戲創建了兩個函數。 我試圖將它們放入 class 中,稱為class GuessTheCountry():

下面的代碼運行沒有問題,但是需要在一個class里面。由於我是Python的新手,而且代碼對我來說已經比較大了,不知道怎么放到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

這些也是要求:

  • 至少 1 個私有和 2 個公共自我屬性
  • 至少 1 個私有方法和 1 個公共方法采用 arguments,返回值並由您的程序使用
  • 一個至少接受 1 個參數的 init() 方法
  • 一個 repr() 方法
  • 證明您的 class 方法按預期工作的單元測試。 測試應該使用斷言語句評估結果。

兩小時后到期,所以我真的需要幫助。

無需給出答案,因為這顯然是針對 class 這可能會幫助您入門。

您可以使用 class 來跟蹤兩個函數中使用的一些值

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM