2 回答

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
我想你已經(jīng)很接近了!您不需要在構(gòu)造函數(shù)方法中使用這些括號(hào)。我刪除了那些。要打印出整個(gè)配方,我們可以簡(jiǎn)單地使用 to string 函數(shù)。根據(jù)需要更改它:
class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
def __str__(self):
output = ''
output += 'Here is the recipe for {}:\n'.format(self.recipe_name)
output += 'You will need: {}\n'.format(self.ingredients)
output += 'This recipe takes: {}\n'.format(self.cook_time)
output += 'Here are the steps involved:\n'
for i, step in enumerate(self.steps):
output += 'Step {}: {}\n'.format(i + 1, step)
return output
你可以運(yùn)行這個(gè):
chicken_noodle = Recipe('Chicken Noodle', ['Broth', 'noodles'], '7 minutes', ['Bring water to boil', 'add broth'])
print (chicken_noodle)
輸出:
Here is the recipe for Chicken Noodle:
You will need: ['Broth', 'noodles']
This recipe takes: 7 minutes
Here are the steps involved:
Step 1: Bring water to boil
Step 2: add broth
添加回答
舉報(bào)