1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

デフォルト引数

Last updated at Posted at 2024-03-06

すみません、デフォルト引数を使わないまま
解答しました。。。

class Ghest():
    def __init__(self):
        #初期化
        self.sum = 0

    def take_food(self, price):
        self.sum += int(price)
    def take_softdrink(self, price):
        self.sum += int(price)

    def take_alcohol(self, price):
        pass
    def get_sum(self):
        return self.sum

class Adult(Ghest):
    def __init__(self):
        super().__init__()
        #初期化
        self.alcohol = False
    def take_food(self, price):
        if self.alcohol:
            self.sum += int(price) - 200
        else:
            self.sum += int(price)
    def take_alcohol(self,price):
        self.sum += int(price)
        self.alcohol = True

N, K = map(int,input().split())
ghests = [None] * N

for i in range(N):
    age = int(input())
    if age >= 20:
        ghests[i] = Adult()
    else:
        ghests[i] = Ghest()

#注文リスト(0=number,1=order,2=price)
for _ in range(K):
    order = input().split()
    if order[1] == "food":
        ghests[int(order[0])-1].take_food(int(order[2]))
    elif order[1] == "softdrink":
        ghests[int(order[0])-1].take_softdrink(int(order[2]))
    elif order[1] == "0":
        ghests[int(order[0])-1].take_alcohol(500)  
    else:
        ghests[int(order[0])-1].take_alcohol(int(order[2]))

#結果
for ghest in ghests:
    print(ghest.get_sum())

デフォルト引数使うと

class Ghest():
    def __init__(self):
        #初期化
        self.sum = 0

    def take_food(self, price):
        self.sum += int(price)
    def take_softdrink(self, price):
        self.sum += int(price)
    #ここでデフォルト引数追加
    def take_alcohol(self, price=500):
        pass
    def get_sum(self):
        return self.sum

class Adult(Ghest):
    def __init__(self):
        super().__init__()
        #初期化
        self.alcohol = False
    def take_food(self, price):
        if self.alcohol:
            self.sum += int(price) - 200
        else:
            self.sum += int(price)
    def take_alcohol(self, price=500):
        self.sum += int(price)
        self.alcohol = True

N, K = map(int,input().split())
ghests = [None] * N

for i in range(N):
    age = int(input())
    if age >= 20:
        ghests[i] = Adult()
    else:
        ghests[i] = Ghest()

#注文リスト(0=number,1=order,2=price)
for _ in range(K):
    order = input().split()
    if order[1] == "food":
        ghests[int(order[0])-1].take_food(int(order[2]))
    elif order[1] == "softdrink":
        ghests[int(order[0])-1].take_softdrink(int(order[2]))
    elif order[1] == "0":
        #ここの引数がなくなった
        ghests[int(order[0])-1].take_alcohol()  
    else:
        ghests[int(order[0])-1].take_alcohol(int(order[2]))

#結果
for ghest in ghests:
    print(ghest.get_sum())

とまあ、こんなふうな変化しかないですが
一応メモで

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?