第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

創(chuàng)建新對(duì)象時(shí)可以使用列表作為屬性嗎?

創(chuàng)建新對(duì)象時(shí)可以使用列表作為屬性嗎?

慕哥6287543 2021-08-14 17:02:37
我一直在學(xué)習(xí) Python,并想開始我的第一個(gè)項(xiàng)目,我今天完成了對(duì)課程的學(xué)習(xí),并想繼續(xù)我對(duì)算法的理解,以及我所學(xué)的一切如何結(jié)合在一起。我想這樣做是因?yàn)槲矣X得這些在線資源為您提供了很好的信息,但沒有教太多如何將這些概念應(yīng)用于項(xiàng)目。我想制作一個(gè)簡(jiǎn)單的程序,我可以在其中輸入食譜名稱,并打印成分、烹飪時(shí)間、步驟和名稱。我想使用成分和步驟列表,我想以列表格式打印它們(可能用邊框包裹)。這可能嗎?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)Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
查看完整描述

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


查看完整回答
反對(duì) 回復(fù) 2021-08-14
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)