import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Primer%3A%20marimo%20and%20the%20Python%20this%20course%20assumes%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20every%20other%20cell%20uses%20(section%202%20explains%20it).%0A%20%20%20%20import%20linecache%0A%20%20%20%20import%20re%0A%20%20%20%20import%20traceback%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20exercise%20checks%2C%20and%20what%20common%20errors%20mean%0A%20%20%20%20from%20course%20import%20check_cases%2C%20try_it%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Primer%3A%20marimo%20and%20the%20Python%20this%20course%20assumes%0A%0A%20%20%20%20This%20primer%20is%20for%20you%20if%20you%20did%20not%20take%20Python%20Essentials%2C%20or%20took%20it%20but%20have%20not%20met%20marimo%2C%20its%20module%207.%20Every%20notebook%20of%20the%20course%20is%20a%20marimo%20notebook%2C%20and%20every%20exercise%20is%20a%20small%20Python%20function%2C%20so%20this%20is%20what%20you%20need%20before%20module%201%3A%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20it%20covers%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20How%20a%20marimo%20notebook%20runs%20%7C%20cells%2C%20results%20above%20the%20code%20and%20cells%20that%20run%20again%20when%20what%20they%20use%20changes%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20One%20name%2C%20one%20cell%20%7C%20why%20a%20name%20is%20made%20in%20one%20cell%20only%3B%20the%20setup%20cell%3B%20functions%20every%20cell%20can%20use%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Text%2C%20controls%20and%20%60mo.stop%60%20%7C%20text%20with%20%60mo.md%60%2C%20sliders%20and%20dropdowns%20and%20a%20cell%20that%20waits%20until%20something%20is%20ready%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20The%20Python%20this%20course%20assumes%20%7C%20values%20and%20types%2C%20lists%20and%20dictionaries%2C%20f-strings%2C%20%60if%60%2C%20%60for%60%2C%20functions%2C%20%60import%60%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20How%20the%20exercises%20work%20%7C%20the%20blank%20function%2C%20its%20check%2C%20Try%20it%20and%20common%20causes%2C%20with%20two%20to%20practice%20on%20%7C%0A%20%20%20%20%7C%206%20%C2%B7%20Reading%20an%20error%20%7C%20what%20Python%20says%20when%20it%20cannot%20go%20on%2C%20read%20from%20its%20last%20line%20%7C%0A%0A%20%20%20%20Come%20back%20to%20any%20part%20at%20any%20time%3B%20nothing%20here%20is%20graded.%20If%20Python%20itself%20is%20new%20to%20you%2C%20section%204%20is%20a%20reminder%2C%20not%20a%20course%3A%20Python%20Essentials%20teaches%20it%20with%20exercises.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20How%20a%20marimo%20notebook%20runs%0A%0A%20%20%20%20A%20marimo%20notebook%20is%20a%20list%20of%20**cells**.%20Most%20cells%20hold%20Python%20code%2C%20and%20the%20result%20of%20a%20cell's%20**last%20line**%20appears%20**above**%20its%20code.%20Many%20of%20the%20course's%20cells%20hide%20their%20code%2C%20so%20the%20page%20reads%20like%20a%20document%3A%20hover%20over%20a%20cell%20and%20click%20the%20%60%3C%2F%3E%60%20icon%20to%20see%20its%20code.%0A%0A%20%20%20%20-%20**Run%20a%20cell%3A**%20click%20inside%20it%20and%20press%20**Ctrl%20%2B%20Enter**%20(Mac%3A%20**Cmd%20%2B%20Enter**)%2C%20or%20click%20its%20%E2%96%B6%20button.%0A%20%20%20%20-%20**Cells%20run%20again%20by%20themselves.**%20marimo%20reads%20each%20cell%20and%20knows%20which%20names%20it%20makes%20and%20which%20names%20it%20uses.%20When%20you%20run%20a%20cell%2C%20every%20cell%20that%20uses%20a%20name%20it%20made%20runs%20again%2C%20then%20the%20cells%20that%20use%20theirs%2C%20in%20the%20right%20order.%20The%20order%20of%20the%20cells%20on%20the%20page%20does%20not%20matter%3B%20what%20each%20one%20uses%20does.%0A%20%20%20%20-%20**Opening%20a%20notebook%20runs%20it%20all**%2C%20so%20the%20results%20are%20there%20when%20it%20opens.%0A%20%20%20%20-%20**Your%20work%20is%20saved**%20as%20you%20go.%20**Ctrl%20%2B%20S**%20(Mac%3A%20**Cmd%20%2B%20S**)%20saves%20at%20once.%0A%0A%20%20%20%20Move%20the%20slider.%20The%20cell%20under%20it%20runs%20again%20because%20it%20uses%20%60members.value%60%3B%20the%20cell%20after%20that%20runs%20again%20because%20it%20uses%20%60area_each%60%2C%20which%20the%20middle%20cell%20makes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20members%20%3D%20mo.ui.slider(start%3D1%2C%20stop%3D12%2C%20value%3D5%2C%20label%3D%22Members%20of%20the%20household%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20members%0A%20%20%20%20return%20(members%2C)%0A%0A%0A%40app.cell%0Adef%20_(members)%3A%0A%20%20%20%20%23%20This%20cell%20uses%20members.value%2C%20so%20it%20runs%20again%20whenever%20the%20slider%20moves%0A%20%20%20%20farm_ha%20%3D%202.5%20%20%23%20the%20household's%20farm%2C%20in%20hectares%0A%20%20%20%20area_each%20%3D%20round(farm_ha%20%2F%20members.value%2C%202)%0A%20%20%20%20area_each%0A%20%20%20%20return%20(area_each%2C)%0A%0A%0A%40app.cell%0Adef%20_(area_each)%3A%0A%20%20%20%20%23%20This%20cell%20uses%20area_each%2C%20so%20it%20runs%20again%20whenever%20the%20cell%20above%20does%0A%20%20%20%20mo.md(f%22Shared%20among%20the%20household's%20members%2C%20the%20farm%20is%20**%7Barea_each%7D%20ha**%20each%2C%20or%20**%7Barea_each%20*%2010_000%3A%2C.0f%7D%20square%20meters**.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**What%20marimo%20does%20not%20see.**%20marimo%20notices%20when%20a%20cell%20gives%20a%20name%20a%20**new%20value**%2C%20not%20when%20a%20value%20is%20changed%20in%20place.%20If%20one%20cell%20makes%20a%20list%20%60crops%60%20and%20another%20cell%20runs%20%60crops.append(%22Banana%22)%60%2C%20the%20cells%20that%20use%20%60crops%60%20do%20not%20run%20again.%20Make%20a%20new%20value%20with%20a%20new%20name%20instead%3A%20%60more_crops%20%3D%20crops%20%2B%20%5B%22Banana%22%5D%60.%20The%20course's%20pandas%20code%20works%20the%20same%20way%3A%20each%20step%20makes%20a%20new%20table.%0A%0A%20%20%20%20%23%23%202%20%C2%B7%20One%20name%2C%20one%20cell%0A%0A%20%20%20%20Because%20marimo%20runs%20cells%20by%20the%20names%20they%20make%20and%20use%2C%20**a%20name%20can%20be%20made%20in%20one%20cell%20only**.%20If%20two%20cells%20both%20say%20%60total%20%3D%20...%60%2C%20marimo%20cannot%20tell%20which%20one%20other%20cells%20mean%2C%20so%20it%20runs%20neither%20and%20says%20*This%20cell%20redefines%20variables%20from%20other%20cells*%2C%20naming%20the%20variable.%20Rename%20one%20of%20them.%0A%0A%20%20%20%20-%20A%20name%20that%20starts%20with%20an%20underscore%2C%20such%20as%20%60_total%60%2C%20belongs%20to%20its%20own%20cell.%20Other%20cells%20cannot%20see%20it%2C%20so%20several%20cells%20can%20each%20have%20their%20own%20%60_total%60.%20The%20course's%20hidden%20cells%20use%20such%20names%20for%20their%20working%20values.%0A%20%20%20%20-%20Deleting%20a%20cell%20deletes%20the%20names%20it%20made%3B%20the%20cells%20that%20used%20them%20say%20so.%0A%20%20%20%20-%20**The%20setup%20cell.**%20At%20the%20top%20of%20every%20notebook%20in%20the%20course%20is%20a%20cell%20marked%20*setup*.%20It%20runs%20first%2C%20and%20holds%20the%20%60import%60%20lines%20every%20other%20cell%20uses%3A%20%60import%20pandas%20as%20pd%60%20there%20makes%20%60pd%60%20usable%20everywhere.%0A%20%20%20%20-%20**Functions%20every%20cell%20can%20use.**%20A%20cell%20that%20holds%20just%20one%20function%2C%20which%20uses%20nothing%20but%20what%20the%20setup%20cell%20imports%2C%20is%20a%20function%20of%20the%20notebook%20itself%3A%20every%20cell%20can%20call%20it%2C%20and%20so%20can%20the%20course's%20checks.%20Every%20exercise%20is%20such%20a%20cell.%0A%0A%20%20%20%20You%20never%20write%20the%20notebook's%20file%20yourself%3A%20marimo%20writes%20it%20as%20you%20edit%2C%20as%20plain%20Python%2C%20so%20it%20can%20be%20read%2C%20compared%20and%20imported%20like%20any%20other%20Python%20file.%20For%20the%20curious%2C%20this%20is%20how%20the%20pieces%20above%20look%20in%20it%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20with%20app.setup%3A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20the%20setup%20cell%0A%20%20%20%20%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20%40app.cell%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20an%20ordinary%20cell%3A%20it%20uses%20members%2C%20and%20makes%20area_each%0A%20%20%20%20def%20_(members)%3A%0A%20%20%20%20%20%20%20%20area_each%20%3D%20round(2.5%20%2F%20members.value%2C%202)%0A%20%20%20%20%20%20%20%20return%20(area_each%2C)%0A%0A%20%20%20%20%40app.function%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20a%20function%20every%20cell%20can%20use%0A%20%20%20%20def%20share(part%2C%20whole)%3A%0A%20%20%20%20%20%20%20%20return%20round(part%20%2F%20whole%20*%20100%2C%201)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%23%23%203%20%C2%B7%20Text%2C%20controls%20and%20%60mo.stop%60%0A%0A%20%20%20%20The%20course's%20cells%20use%20a%20few%20of%20marimo's%20own%20tools%2C%20all%20from%20%60mo%60%20(%60import%20marimo%20as%20mo%60%2C%20in%20the%20setup%20cell)%3A%0A%0A%20%20%20%20%7C%20tool%20%7C%20what%20it%20does%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60mo.md(%22...%22)%60%20%7C%20shows%20text%20written%20in%20**Markdown**%3A%20%60**bold**%60%2C%20%60*italic*%60%2C%20%60%60%20%60code%60%20%60%60%2C%20%60%23%23%23%20headings%60%2C%20tables.%20With%20an%20f-string%2C%20values%20go%20into%20the%20text%3A%20%60mo.md(f%22**%7Bn%7D**%20households%22)%60%20%7C%0A%20%20%20%20%7C%20%60mo.ui.slider(...)%60%2C%20%60mo.ui.dropdown(...)%60%2C%20%60mo.ui.text(...)%60%2C%20%60mo.ui.checkbox(...)%60%2C%20%60mo.ui.run_button(...)%60%20%7C%20**controls**%2C%20which%20marimo%20calls%20UI%20elements.%20Give%20one%20a%20name%20and%20make%20it%20a%20cell's%20last%20line%20to%20show%20it%3B%20its%20current%20value%20is%20%60.value%60.%20Every%20cell%20that%20uses%20it%20runs%20again%20when%20you%20change%20it%2C%20so%20a%20control%20is%20made%20in%20one%20cell%20and%20used%20in%20another%20%7C%0A%20%20%20%20%7C%20%60mo.vstack(%5B...%5D)%60%2C%20%60mo.hstack(%5B...%5D)%60%20%7C%20shows%20several%20things%20in%20one%20cell%2C%20one%20under%20the%20other%20or%20side%20by%20side%20%7C%0A%20%20%20%20%7C%20%60mo.stop(condition%2C%20output)%60%20%7C%20stops%20the%20cell%20when%20%60condition%60%20is%20true%2C%20and%20shows%20%60output%60%20instead%20of%20its%20result.%20The%20cells%20that%20use%20what%20it%20makes%20wait%20too%20%7C%0A%20%20%20%20%7C%20%60mo.accordion(%7B...%7D)%60%20%7C%20a%20folded%20section%20that%20opens%20on%20a%20click%3A%20the%20course's%20*common%20causes*%20tables%20%7C%0A%0A%20%20%20%20The%20course%20uses%20%60mo.stop%60%20wherever%20a%20cell%20needs%20an%20exercise%20you%20have%20not%20finished%3A%20it%20shows%20*This%20part%20uses%20Exercise%203.2.%20Finish%20it%20above%2C%20and%20this%20cell%20runs%20by%20itself.*%20Here%20it%20is%20with%20a%20dropdown%20that%20starts%20empty%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20province%20%3D%20mo.ui.dropdown(options%3D%5B%22Ilocos%20Norte%22%2C%20%22Isabela%22%2C%20%22Nueva%20Ecija%22%2C%20%22Iloilo%22%5D%2C%20label%3D%22A%20province%3A%22)%0A%20%20%20%20province%0A%20%20%20%20return%20(province%2C)%0A%0A%0A%40app.cell%0Adef%20_(province)%3A%0A%20%20%20%20mo.stop(province.value%20is%20None%2C%20mo.md(%22*Pick%20a%20province%20above%3A%20this%20cell%20waits%20until%20you%20do.*%22))%0A%20%20%20%20mo.md(f%22You%20picked%20**%7Bprovince.value%7D**.%20Pick%20another%2C%20and%20this%20text%20changes%20by%20itself.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20The%20Python%20this%20course%20assumes%0A%0A%20%20%20%20The%20course%20builds%20on%20these%2C%20from%20Python%20Essentials.%20Each%20row's%20last%20column%20is%20what%20Python%20gives%2C%20worked%20out%20as%20this%20notebook%20runs%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20python_table()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20basics%2C%20as%20a%20Markdown%20table%3A%20each%20example%20run%2C%20with%20what%20Python%20gives%20for%20it.%22%22%22%0A%20%20%20%20names%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22household%22%3A%20%7B%22hh_id%22%3A%20%22HH-0001%22%2C%20%22province%22%3A%20%22Ilocos%20Norte%22%2C%20%22hh_size%22%3A%207%2C%20%22head_age%22%3A%2058%7D%2C%0A%20%20%20%20%20%20%20%20%22crops%22%3A%20%5B%22Rice%22%2C%20%22Corn%22%2C%20%22Vegetables%22%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22values%20and%20their%20types%22%2C%20'type(7)%2C%20type(2.5)%2C%20type(%220102820000%22)%2C%20type(True)%2C%20type(None)'%2C%0A%20%20%20%20%20%20%20%20%20%22every%20value%20has%20a%20type%3A%20a%20whole%20number%20(%60int%60)%2C%20a%20decimal%20(%60float%60)%2C%20text%20(%60str%60)%2C%20yes%20or%20no%20(%60bool%60)%2C%20nothing%20(%60None%60)%22)%2C%0A%20%20%20%20%20%20%20%20(%22text%20keeps%20every%20character%22%2C%20'%220102820000%22%2C%20int(%220102820000%22)'%2C%20%22a%20code%20is%20text%3B%20as%20a%20number%20it%20loses%20its%20leading%20zero%22)%2C%0A%20%20%20%20%20%20%20%20(%22arithmetic%22%2C%20%221640%20%2F%200.439%2C%207%20%2F%2F%202%2C%207%20%25%202%2C%20round(3735.763%2C%201)%22%2C%20%22%60%2F%60%20divides%2C%20%60%2F%2F%60%20gives%20the%20whole%20part%2C%20%60%25%60%20what%20is%20left%20over%22)%2C%0A%20%20%20%20%20%20%20%20(%22a%20list%3A%20values%20in%20order%22%2C%20'crops%5B0%5D%2C%20crops%5B-1%5D%2C%20len(crops)%2C%20crops%20%2B%20%5B%22Banana%22%5D'%2C%20%22positions%20count%20from%200%3B%20%60-1%60%20is%20the%20last%22)%2C%0A%20%20%20%20%20%20%20%20(%22a%20dictionary%3A%20keys%20and%20their%20values%22%2C%20'household%5B%22hh_size%22%5D%2C%20household.get(%22weight%22)%2C%20list(household)'%2C%20%22look%20a%20value%20up%20by%20its%20key%3B%20%60.get%60%20gives%20%60None%60%20for%20a%20missing%20key%22)%2C%0A%20%20%20%20%20%20%20%20(%22comparisons%22%2C%20'household%5B%22hh_size%22%5D%20%3E%3D%206%2C%20household%5B%22province%22%5D%20%3D%3D%20%22Iloilo%22%2C%20%22Corn%22%20in%20crops'%2C%20%22each%20gives%20%60True%60%20or%20%60False%60%22)%2C%0A%20%20%20%20%20%20%20%20(%22an%20f-string%22%2C%20'f%22%7Bhousehold%5B%5C'hh_id%5C'%5D%7D%20has%20%7Bhousehold%5B%5C'hh_size%5C'%5D%7D%20members%22'%2C%20%22values%20in%20%60%7B%20%7D%60%20are%20put%20into%20the%20text%22)%2C%0A%20%20%20%20%20%20%20%20(%22text%20methods%22%2C%20'%220102820000%22%5B%3A2%5D%2C%20%221%2C640%22.replace(%22%2C%22%2C%20%22%22)%2C%20%22Rice%3B%20Corn%22.split(%22%3B%20%22)%2C%20%2228%22.zfill(3)'%2C%20%22a%20slice%2C%20a%20replacement%2C%20a%20split%2C%20zeros%20on%20the%20left%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20def%20shown(value)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20if%20isinstance(value%2C%20type)%3A%20%20%23%20a%20type%2C%20by%20its%20name%3A%20int%2C%20not%20%3Cclass%20'int'%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20value.__name__%0A%20%20%20%20%20%20%20%20if%20isinstance(value%2C%20tuple)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22(%22%20%2B%20%22%2C%20%22.join(shown(v)%20for%20v%20in%20value)%20%2B%20%22)%22%0A%20%20%20%20%20%20%20%20return%20repr(value)%0A%0A%20%20%20%20lines%20%3D%20%5B%22%7C%20idea%20%7C%20example%20%7C%20Python%20gives%20%7C%20what%20to%20notice%20%7C%22%2C%20%22%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%22%5D%0A%20%20%20%20for%20idea%2C%20code%2C%20note%20in%20rows%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20eval(code%2C%20%7B%7D%2C%20dict(names))%20%20%23%20the%20course's%20own%20examples%2C%20run%20here%20so%20the%20table%20is%20always%20right%0A%20%20%20%20%20%20%20%20lines.append(f%22%7C%20%7Bidea%7D%20%7C%20%60%7Bcode%7D%60%20%7C%20%60%7Bshown(result)%7D%60%20%7C%20%7Bnote%7D%20%7C%22)%0A%20%20%20%20return%20%22%5Cn%22.join(lines)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22The%20examples%20use%20two%20names%3A%20%60household%20%3D%20%7B'hh_id'%3A%20'HH-0001'%2C%20'province'%3A%20'Ilocos%20Norte'%2C%20'hh_size'%3A%207%2C%20'head_age'%3A%2058%7D%60%20and%20%60crops%20%3D%20%5B'Rice'%2C%20'Corn'%2C%20'Vegetables'%5D%60.%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%2B%20python_table()%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Decisions%2C%20loops%2C%20functions%20and%20imports%20are%20statements%20of%20several%20lines.%20The%20cell%20below%20has%20one%20of%20each%3B%20change%20%60%22hh_size%22%3A%207%60%20to%20%604%60%2C%20or%20add%20a%20plot%20to%20%60plot_areas%60%2C%20and%20run%20it.%0A%0A%20%20%20%20-%20%60if%60%20runs%20its%20indented%20lines%20only%20when%20its%20condition%20is%20%60True%60%3B%20%60else%60%20runs%20when%20it%20is%20not.%20The%20**indentation**%20(four%20spaces)%20is%20how%20Python%20knows%20which%20lines%20belong%20to%20which%20part.%0A%20%20%20%20-%20%60for%60%20runs%20its%20indented%20lines%20once%20for%20each%20value%20of%20a%20list%2C%20with%20a%20name%20for%20the%20current%20value.%0A%20%20%20%20-%20%60def%60%20makes%20a%20**function**%3A%20a%20name%2C%20its%20inputs%20in%20brackets%20and%20indented%20steps%3B%20%60return%60%20hands%20the%20result%20back%20to%20whoever%20called%20it.%0A%20%20%20%20-%20%60import%60%20brings%20in%20code%20from%20elsewhere%3A%20%60import%20pandas%20as%20pd%60%20makes%20pandas%20usable%20as%20%60pd%60%3B%20%60from%20course%20import%20PATHS%60%20brings%20in%20one%20name%20from%20the%20course's%20%60course.py%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20household%20%3D%20%7B%22hh_id%22%3A%20%22HH-0001%22%2C%20%22province%22%3A%20%22Ilocos%20Norte%22%2C%20%22hh_size%22%3A%207%2C%20%22head_age%22%3A%2058%7D%0A%20%20%20%20plot_areas%20%3D%20%5B0.439%2C%200.194%5D%20%20%23%20the%20household's%20plots%2C%20in%20hectares%0A%0A%20%20%20%20if%20household%5B%22hh_size%22%5D%20%3E%3D%206%3A%20%20%23%20a%20decision%0A%20%20%20%20%20%20%20%20size_class%20%3D%20%22large%22%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20size_class%20%3D%20%22not%20large%22%0A%0A%20%20%20%20total_area%20%3D%200%0A%20%20%20%20for%20area%20in%20plot_areas%3A%20%20%23%20a%20loop%3A%20once%20for%20each%20plot%0A%20%20%20%20%20%20%20%20total_area%20%3D%20total_area%20%2B%20area%0A%0A%0A%20%20%20%20def%20per_person(amount%2C%20people)%3A%20%20%23%20a%20function%3A%20two%20inputs%2C%20one%20result%0A%20%20%20%20%20%20%20%20return%20round(amount%20%2F%20people%2C%203)%0A%0A%0A%20%20%20%20f%22%7Bhousehold%5B'hh_id'%5D%7D%20(%7Bhousehold%5B'province'%5D%7D)%20is%20a%20%7Bsize_class%7D%20household%20farming%20%7Btotal_area%3A.3f%7D%20ha%2C%20%7Bper_person(total_area%2C%20household%5B'hh_size'%5D)%7D%20ha%20per%20person.%22%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20this%20course%20you%20will%20write%20fewer%20loops%20than%20in%20Python%20Essentials%3A%20NumPy%20and%20pandas%20compute%20on%20a%20whole%20column%20at%20once%20(module%202%20shows%20why%20that%20is%20faster%20and%20shorter).%20Functions%2C%20dictionaries%2C%20f-strings%20and%20%60if%60%20stay%20everywhere.%0A%0A%20%20%20%20%23%23%205%20%C2%B7%20How%20the%20exercises%20work%0A%0A%20%20%20%20Every%20exercise%20has%20the%20same%20parts%2C%20one%20under%20the%20other%3A%0A%0A%20%20%20%201.%20**The%20task**%2C%20as%20text%3A%20why%20it%20matters%2C%20what%20goes%20in%20and%20what%20comes%20out%20(**Input%20%E2%86%92%20output**)%2C%20an%20example%20and%20the%20tools%20to%20use%20with%20where%20they%20were%20taught.%0A%20%20%20%202.%20**The%20function%20to%20finish**%2C%20in%20a%20code%20cell.%20Its%20name%2C%20its%20inputs%2C%20a%20docstring%20and%20step-by-step%20comments%20are%20written%20for%20you%2C%20and%20it%20ends%20with%3A%0A%0A%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20raise%20NotImplementedError(%22Exercise%201.1%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20That%20line%20means%20*not%20written%20yet*%3A%20it%20stops%20the%20function%20on%20purpose.%20Write%20your%20code%20under%20the%20steps%2C%20**delete%20that%20line**%20and%20run%20the%20cell.%0A%20%20%20%203.%20**The%20check**%2C%20in%20the%20box%20under%20the%20function.%20It%20**runs%20your%20function**%20on%20several%20inputs%20the%20course%20chose%2C%20and%20compares%20each%20result%20with%20what%20a%20correct%20function%20gives%3A%20green%20when%20all%20match%3B%20red%20with%20the%20first%20input%20that%20does%20not%2C%20and%20what%20your%20function%20gave%20for%20it%3B%20grey%20while%20the%20function%20is%20not%20written.%20It%20never%20shows%20the%20right%20answer%2C%20only%20where%20yours%20differs%2C%20because%20the%20goal%20is%20a%20function%20that%20works%20for%20**any**%20input.%20Returning%20a%20fixed%20value%20that%20suits%20one%20example%20fails%20the%20others.%0A%20%20%20%204.%20**Try%20it**%2C%20a%20cell%20with%20its%20code%20showing%3A%20it%20calls%20your%20function%20on%20the%20real%20data%20and%20shows%20what%20it%20gives%20back.%20If%20your%20function%20raises%20an%20error%2C%20Try%20it%20names%20the%20error%20and%20says%20what%20it%20usually%20means%20(section%206).%20Change%20its%20inputs%20to%20try%20your%20own.%0A%20%20%20%205.%20**Common%20causes**%2C%20a%20folded%20table%20under%20the%20check%3A%20what%20you%20see%2C%20the%20usual%20cause%20and%20where%20to%20look.%20Open%20it%20when%20the%20check%20stays%20red.%0A%20%20%20%206.%20**The%20companion**%3A%20when%20the%20check%20is%20green%2C%20it%20links%20to%20the%20course's%20solution%20and%20why%20it%20works%2C%20on%20the%20Learn%20Python%20site%2C%20to%20compare%20with%20yours.%20When%20you%20are%20stuck%2C%20the%20red%20check%20links%20there%20too.%0A%0A%20%20%20%20%60return%60%2C%20not%20%60print%60%3A%20the%20check%20looks%20at%20what%20your%20function%20**returns**.%20%60print%60%20only%20shows%20text%20on%20the%20screen%2C%20and%20the%20function%20then%20returns%20%60None%60.%0A%0A%20%20%20%20%23%23%23%20Exercise%20P1.1.%20One%20line%20about%20a%20household%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20whole%20rhythm%20of%20the%20course's%20exercises%2C%20on%20Python%20you%20know%3A%20a%20dictionary%2C%20an%20f-string%20and%20%60return%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20household%20as%20a%20dictionary%20with%20%60%22hh_id%22%60%2C%20%60%22province%22%60%20and%20%60%22hh_size%22%60%20%E2%86%92%20one%20line%20of%20text%20about%20it.%0A%0A%20%20%20%20%7C%20%60household%60%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%7B%22hh_id%22%3A%20%22HH-0001%22%2C%20%22province%22%3A%20%22Ilocos%20Norte%22%2C%20%22hh_size%22%3A%207%7D%60%20%7C%20%60%22HH-0001%2C%20Ilocos%20Norte%2C%20household%20size%207%22%60%20%7C%0A%20%20%20%20%7C%20%60%7B%22hh_id%22%3A%20%22HH-0412%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22hh_size%22%3A%201%7D%60%20%7C%20%60%22HH-0412%2C%20Iloilo%2C%20household%20size%201%22%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60household%5B%22hh_id%22%5D%60%20looks%20up%20a%20value%20by%20its%20key%3B%20an%20f-string%20puts%20values%20into%20text%20(section%204).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20household_line(household%3A%20dict)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22One%20line%20of%20text%20about%20a%20household%3A%20its%20ID%2C%20its%20province%20and%20its%20size.%22%22%22%0A%20%20%20%20%23%20Exercise%20P1.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20look%20up%20the%20household's%20hh_id%2C%20province%20and%20hh_size%0A%20%20%20%20%23%20Step%202%3A%20return%20them%20in%20an%20f-string%3A%20%22%3Chh_id%3E%2C%20%3Cprovince%3E%2C%20household%20size%20%3Chh_size%3E%22%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P1.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20P1.1%22%2C%20household_line%2C%0A%20%20%20%20%20%20%20%20%5B(f%22household_line(%7Bh!r%7D)%22%2C%20(h%2C))%20for%20h%20in%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22HH-0001%22%2C%20%22province%22%3A%20%22Ilocos%20Norte%22%2C%20%22hh_size%22%3A%207%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22HH-0412%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22hh_size%22%3A%201%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22HH-1188%22%2C%20%22province%22%3A%20%22Cotabato%22%2C%20%22hh_size%22%3A%2012%2C%20%22weight%22%3A%20640.5%7D%2C%0A%20%20%20%20%20%20%20%20%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22households%22%2C%20hint%3D'Return%20the%20f-string%3B%20compare%20its%20commas%20and%20spaces%20with%20%22HH-0001%2C%20Ilocos%20Norte%2C%20household%20size%207%22.'%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20one%20line%20about%20the%20first%20household.%20Change%20a%20value%20and%20run%20the%20cell.%0A%20%20%20%20try_it(household_line%2C%20%7B%22hh_id%22%3A%20%22HH-0001%22%2C%20%22province%22%3A%20%22Ilocos%20Norte%22%2C%20%22hh_size%22%3A%207%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%20P1.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60None%60%20%7C%20The%20function%20prints%20the%20line%20instead%20of%20returning%20it.%20%7C%20%60return%20f%22...%22%60%20%7C%0A%20%20%20%20%7C%20Text%20that%20differs%20by%20a%20comma%20or%20a%20space%20%7C%20The%20f-string's%20fixed%20parts%20differ%20from%20the%20examples.%20%7C%20%60%22%2C%20%22%60%20after%20the%20ID%20and%20the%20province%2C%20%60household%20size%20%60%20before%20the%20size%20%7C%0A%20%20%20%20%7C%20%60%7Bhousehold%5B'hh_id'%5D%7D%60%20in%20the%20result%2C%20braces%20and%20all%20%7C%20The%20%60f%60%20in%20front%20of%20the%20text%20is%20missing%2C%20so%20nothing%20is%20filled%20in.%20%7C%20%60f%22...%22%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'HH_ID'*%20%7C%20A%20key%20spelled%20differently%20from%20the%20dictionary's.%20%7C%20%60%22hh_id%22%60%2C%20%60%22province%22%60%2C%20%60%22hh_size%22%60%20%7C%0A%20%20%20%20%7C%20The%20same%20text%20for%20every%20household%20%7C%20The%20values%20are%20typed%20in%20instead%20of%20looked%20up.%20%7C%20%60household%5B...%5D%60%20for%20each%20value%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%20P1.2.%20The%20mean%20age%2C%20without%20the%20code%20for%20%22not%20stated%22%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20in%20the%20farm%20survey%2C%20an%20age%20of%20%60999%60%20means%20*not%20stated*%2C%20not%20an%20age.%20A%20mean%20that%20includes%20it%20is%20wrong%2C%20with%20no%20error%20to%20warn%20you%3B%20module%201%20shows%20how%20far%20off.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20list%20of%20ages%20%E2%86%92%20the%20mean%20of%20the%20ages%20that%20are%20not%20%60999%60%2C%20rounded%20to%20one%20decimal%3B%20%60None%60%20when%20no%20age%20is%20stated%20(all%20are%20%60999%60%2C%20or%20the%20list%20is%20empty).%0A%0A%20%20%20%20%7C%20%60ages%60%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%5B58%2C%2042%2C%20999%2C%2040%5D%60%20%7C%20%6046.7%60%3A%20(58%20%2B%2042%20%2B%2040)%20%2F%203%20%7C%0A%20%20%20%20%7C%20%60%5B999%2C%20999%5D%60%20%7C%20%60None%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20list%20that%20starts%20empty%2C%20%60for%60%20and%20%60if%60%20to%20fill%20it%2C%20%60.append(value)%60%20to%20add%20to%20it%3B%20%60sum(numbers)%60%20and%20%60len(numbers)%60%3B%20%60round(value%2C%201)%60%3B%20%60if%20not%20stated%3A%60%20is%20true%20when%20the%20list%20%60stated%60%20is%20empty.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20mean_age(ages%3A%20list%5Bint%5D)%20-%3E%20float%20%7C%20None%3A%0A%20%20%20%20%22%22%22The%20mean%20of%20the%20stated%20ages%20(999%20means%20not%20stated)%2C%20to%20one%20decimal%3B%20None%20when%20no%20age%20is%20stated.%22%22%22%0A%20%20%20%20%23%20Exercise%20P1.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20ages%20that%20are%20not%20999%2C%20in%20a%20new%20list%0A%20%20%20%20%23%20Step%202%3A%20if%20that%20list%20is%20empty%2C%20return%20None%0A%20%20%20%20%23%20Step%203%3A%20otherwise%2C%20return%20its%20mean%20(sum%20divided%20by%20count)%2C%20rounded%20to%20one%20decimal%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P1.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20P1.2%22%2C%20mean_age%2C%0A%20%20%20%20%20%20%20%20%5B(f%22mean_age(%7Ba!r%7D)%22%2C%20(a%2C))%20for%20a%20in%20%5B%5B58%2C%2042%2C%20999%2C%2040%5D%2C%20%5B999%2C%20999%5D%2C%20%5B%5D%2C%20%5B30%5D%2C%20%5B61%2C%20999%2C%2047%2C%20999%2C%2052%5D%2C%20%5B35%2C%2036%5D%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20ages%22%2C%20hint%3D%22Leave%20out%20every%20999%20before%20taking%20the%20mean%2C%20and%20return%20None%20when%20no%20age%20is%20left.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20mean%20age%20of%20four%20household%20heads%2C%20one%20of%20them%20not%20stated.%20Change%20the%20list%20and%20run%20the%20cell.%0A%20%20%20%20try_it(mean_age%2C%20%5B58%2C%2042%2C%20999%2C%2040%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%20P1.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60284.8%60%20for%20%60%5B58%2C%2042%2C%20999%2C%2040%5D%60%20%7C%20999%20is%20counted%20as%20an%20age.%20%7C%20keep%20only%20the%20ages%20that%20are%20not%20999%20before%20the%20mean%20%7C%0A%20%20%20%20%7C%20%6035.0%60%20for%20%60%5B58%2C%2042%2C%20999%2C%2040%5D%60%20%7C%20The%20sum%20leaves%20999%20out%2C%20but%20the%20count%20does%20not%3A%20140%20is%20divided%20by%204.%20%7C%20%60len(stated)%60%2C%20not%20%60len(ages)%60%20%7C%0A%20%20%20%20%7C%20*ZeroDivisionError%3A%20division%20by%20zero*%20%7C%20No%20age%20is%20stated%2C%20so%20the%20mean%20divides%20by%200.%20%7C%20%60if%20not%20stated%3A%20return%20None%60%2C%20before%20dividing%20%7C%0A%20%20%20%20%7C%20%6046.666667%60%20%7C%20The%20mean%20is%20not%20rounded.%20%7C%20%60round(...%2C%201)%60%20%7C%0A%20%20%20%20%7C%20%60None%60%20for%20every%20list%20%7C%20%60return%20None%60%20runs%20every%20time%3A%20it%20is%20not%20inside%20the%20%60if%60%2C%20or%20the%20%60if%60%20tests%20the%20wrong%20list.%20%7C%20the%20indentation%20of%20%60return%20None%60%20under%20%60if%20not%20stated%3A%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%206%20%C2%B7%20Reading%20an%20error%0A%0A%20%20%20%20When%20Python%20cannot%20go%20on%2C%20it%20stops%20and%20prints%20an%20**error%20message**%2C%20also%20called%20a%20**traceback**.%20In%20marimo%20it%20appears%20in%20the%20cell's%20output%2C%20in%20red.%20It%20looks%20alarming%2C%20but%20it%20says%2C%20as%20exactly%20as%20Python%20can%2C%20what%20went%20wrong%20and%20where.%20Read%20it%20in%20this%20order%3A%0A%0A%20%20%20%201.%20**The%20last%20line%20first.**%20It%20names%20the%20**kind**%20of%20error%20and%20the%20value%20involved%3A%20%60KeyError%3A%20'weigth'%60%20means%20there%20is%20no%20column%20called%20%60weigth%60.%0A%20%20%20%202.%20**Then%20the%20last%20of%20your%20own%20lines%20above%20it.**%20It%20is%20the%20line%20of%20your%20code%20that%20failed%2C%20often%20with%20%60~~~%5E%5E%5E%60%20under%20the%20part%20that%20did.%20Lines%20from%20files%20inside%20%60site-packages%60%20are%20a%20library's%20own%20code%20(pandas'%2C%20for%20example)%3A%20skip%20them.%0A%20%20%20%203.%20**The%20lines%20further%20up**%20show%20how%20Python%20got%20there%3A%20which%20function%20called%20which.%20You%20rarely%20need%20them.%0A%0A%20%20%20%20When%20the%20error%20comes%20from%20inside%20a%20library%2C%20the%20message%20can%20hold%20**two**%20tracebacks%20joined%20by%20*The%20above%20exception%20was%20the%20direct%20cause%20of%20the%20following%20exception*.%20Read%20the%20last%20one%3B%20its%20last%20line%20is%20still%20the%20answer.%20A%20few%20errors%20add%20lines%20of%20detail%20after%20the%20line%20that%20names%20them%2C%20as%20pandas'%20%60MergeError%60%20does%3A%20read%20from%20that%20line.%0A%0A%20%20%20%20Pick%20a%20line%20of%20code%20that%20fails%2C%20and%20read%20what%20Python%20says%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20broken_examples()%20-%3E%20dict%5Bstr%2C%20str%5D%3A%0A%20%20%20%20%22%22%22Lines%20of%20code%20that%20fail%2C%20each%20with%20the%20kind%20of%20error%20it%20raises%2C%20for%20the%20explainer%20below.%22%22%22%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22KeyError%3A%20a%20column%20that%20is%20not%20in%20the%20table%22%3A%20'households%5B%22weigth%22%5D.sum()'%2C%0A%20%20%20%20%20%20%20%20%22TypeError%3A%20text%20and%20a%20number%20added%22%3A%20'size%20%3D%207%5Cnlabel%20%3D%20%22Household%20of%20%22%20%2B%20size'%2C%0A%20%20%20%20%20%20%20%20%22ValueError%3A%20text%20that%20is%20not%20a%20whole%20number%22%3A%20'int(%221%2C640%22)'%2C%0A%20%20%20%20%20%20%20%20%22NameError%3A%20a%20name%20that%20does%20not%20exist%22%3A%20'total%20%3D%205%5Cntotl%20%2B%201'%2C%0A%20%20%20%20%20%20%20%20%22AttributeError%3A%20a%20text%20method%20on%20a%20number%22%3A%20'code%20%3D%20102820000%5Cncode.zfill(10)'%2C%0A%20%20%20%20%20%20%20%20%22IndexError%3A%20past%20the%20end%20of%20a%20list%22%3A%20'sizes%20%3D%20%5B7%2C%202%2C%208%5D%5Cnsizes%5B3%5D'%2C%0A%20%20%20%20%20%20%20%20%22SyntaxError%3A%20a%20missing%20colon%22%3A%20'size%20%3D%207%5Cnif%20size%20%3E%205%5Cn%20%20%20%20label%20%3D%20%22large%22'%2C%0A%20%20%20%20%20%20%20%20%22IndentationError%3A%20a%20block%20not%20indented%22%3A%20'size%20%3D%207%5Cnif%20size%20%3E%205%3A%5Cnlabel%20%3D%20%22large%22'%2C%0A%20%20%20%20%20%20%20%20%22MergeError%3A%20a%20join%20whose%20key%20repeats%22%3A%20'plots%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0001%22%5D%2C%20%22plot_no%22%3A%20%5B1%2C%202%5D%7D)%5Cnplots.merge(households%2C%20on%3D%22hh_id%22%2C%20validate%3D%22one_to_one%22)'%2C%0A%20%20%20%20%20%20%20%20%22NotImplementedError%3A%20an%20exercise%20not%20written%20yet%22%3A%20'def%20code_meanings(values)%3A%5Cn%20%20%20%20raise%20NotImplementedError(%22Exercise%201.1%22)%5Cn%5Cncode_meanings(%221%20%3D%20Male%22)'%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.function%0Adef%20run_broken(code%3A%20str)%20-%3E%20tuple%5Bstr%2C%20str%5D%3A%0A%20%20%20%20%22%22%22Run%20a%20line%20of%20code%20that%20fails%2C%20as%20a%20cell%20would%2C%20and%20give%20back%20Python's%20message%20and%20the%20kind%20of%20error.%22%22%22%0A%20%20%20%20name%20%3D%20%22%3Cyour%20cell%3E%22%0A%20%20%20%20linecache.cache%5Bname%5D%20%3D%20(len(code)%2C%20None%2C%20code.splitlines(keepends%3DTrue)%2C%20name)%20%20%23%20so%20the%20message%20can%20show%20your%20line%0A%20%20%20%20households%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0002%22%5D%2C%20%22province%22%3A%20%5B%22Ilocos%20Norte%22%2C%20%22Ilocos%20Norte%22%5D%2C%20%22weight%22%3A%20%5B279.77%2C%20349.61%5D%7D)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20exec(compile(code%2C%20name%2C%20%22exec%22)%2C%20%7B%22pd%22%3A%20pd%2C%20%22households%22%3A%20households%7D)%0A%20%20%20%20except%20Exception%20as%20error%3A%0A%20%20%20%20%20%20%20%20report%20%3D%20traceback.TracebackException.from_exception(error)%0A%20%20%20%20%20%20%20%20for%20part%20in%20(report%2C%20report.__cause__%2C%20report.__context__)%3A%20%20%23%20leave%20out%20this%20function's%20own%20line%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20part%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20part.stack%20%3D%20traceback.StackSummary.from_list(%5Bf%20for%20f%20in%20part.stack%20if%20f.name%20!%3D%20%22run_broken%22%5D)%0A%20%20%20%20%20%20%20%20text%20%3D%20%22%22.join(report.format())%0A%20%20%20%20%20%20%20%20text%20%3D%20re.sub(r'File%20%22%5B%5E%22%5D*site-packages%5B%2F%5C%5C%5D'%2C%20'File%20%22...%2Fsite-packages%2F'%2C%20text)%20%20%23%20shorten%20the%20libraries'%20paths%0A%20%20%20%20%20%20%20%20return%20text.rstrip()%2C%20type(error).__name__%0A%20%20%20%20return%20%22(it%20ran%20without%20an%20error)%22%2C%20%22%22%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20broken%20%3D%20mo.ui.dropdown(options%3Dlist(broken_examples())%2C%20value%3D%22KeyError%3A%20a%20column%20that%20is%20not%20in%20the%20table%22%2C%20label%3D%22A%20line%20that%20fails%3A%22)%0A%20%20%20%20broken%0A%20%20%20%20return%20(broken%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(broken)%3A%0A%20%20%20%20_code%20%3D%20broken_examples()%5Bbroken.value%5D%0A%20%20%20%20_message%2C%20_kind%20%3D%20run_broken(_code)%0A%20%20%20%20_meaning%20%3D%20course.ERROR_MEANINGS.get(_kind%2C%20%22read%20the%20message%3A%20it%20usually%20names%20the%20value%20or%20line%20that%20went%20wrong.%22)%0A%20%20%20%20_named%20%3D%20%5Bline%20for%20line%20in%20_message.splitlines()%20if%20re.match(rf%22%5E%5B%5Cw.%5D*%7B_kind%7D%3A%22%2C%20line)%5D%20%20%23%20the%20line%20that%20names%20the%20error%0A%20%20%20%20_last%20%3D%20_named%5B-1%5D%20if%20_named%20else%20_message.splitlines()%5B-1%5D%0A%20%20%20%20_more%20%3D%20%22%22%20if%20_last%20%3D%3D%20_message.splitlines()%5B-1%5D%20else%20%22%20pandas%20adds%20some%20lines%20of%20detail%20after%20it%2C%20the%20rows%20whose%20key%20repeats%3A%20read%20from%20the%20line%20that%20names%20the%20error.%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**The%20code**%20(here%2C%20%60households%60%20is%20a%20small%20table%20with%20the%20columns%20%60hh_id%60%2C%20%60province%60%20and%20%60weight%60)%3A%5Cn%5Cn%60%60%60python%5Cn%7B_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**What%20Python%20says%3A**%5Cn%5Cn%60%60%60text%5Cn%7B_message%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22**The%20last%20line%3A**%20%60%7B_last%7D%60.%7B_more%7D%5Cn%5Cn**%7B_kind%7D**%20usually%20means%3A%20%7B_meaning%7D%22)%2C%20kind%3D%22info%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_rows%20%3D%20%5Bf%22%7C%20%60%7Bkind%7D%60%20%7C%20%7Bmeaning%7D%20%7C%22%20for%20kind%2C%20meaning%20in%20course.ERROR_MEANINGS.items()%5D%0A%20%20%20%20_rows.append('%7C%20%60KeyError%60%20from%20a%20pandas%20table%20%7C%20%60table%5B%22weigth%22%5D%60%3A%20the%20table%20has%20no%20column%20of%20that%20name.%20Compare%20the%20name%20with%20%60table.columns%60%2C%20letter%20by%20letter%3A%20names%20are%20exact%2C%20lower%20case%20and%20spaces%20included.%20%7C')%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22**The%20errors%20you%20will%20meet%20most%20often%2C%20and%20what%20they%20usually%20mean.**%20The%20Try%20it%20cells%20explain%20an%20error%20in%20these%20same%20words.%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20error%20%7C%20what%20it%20usually%20means%20%7C%5Cn%7C%3A--%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(_rows)%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**What%20to%20do%2C%20in%20order**%0A%0A%20%20%20%201.%20Read%20the%20last%20line%2C%20then%20your%20own%20line%20above%20it.%0A%20%20%20%202.%20Try%20your%20function%20on%20one%20small%20input%2C%20in%20the%20**Try%20it**%20cell%20under%20the%20exercise%2C%20and%20look%20at%20what%20it%20gives%20back.%0A%20%20%20%203.%20Look%20at%20the%20values%20on%20the%20way%3A%20add%20a%20line%20such%20as%20%60print(table.columns)%60%20or%20%60print(value%2C%20type(value))%60%20before%20the%20line%20that%20fails%2C%20run%20the%20cell%20and%20read%20what%20it%20prints.%20Remove%20the%20line%20afterwards.%0A%20%20%20%204.%20Open%20the%20exercise's%20**common%20causes**.%0A%20%20%20%205.%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%0A%20%20%20%20**marimo's%20own%20messages**%0A%0A%20%20%20%20%7C%20marimo%20says%20%7C%20it%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*This%20cell%20redefines%20variables%20from%20other%20cells*%20%7C%20two%20cells%20make%20the%20same%20name%20(section%202)%3A%20rename%20one%2C%20or%20start%20it%20with%20%60_%60%20%7C%0A%20%20%20%20%7C%20*This%20cell%20wasn't%20run%20because%20an%20ancestor%20was%20stopped%20with%20%60mo.stop%60*%20%7C%20a%20cell%20this%20one%20needs%20is%20waiting%20(section%203)%2C%20usually%20for%20an%20exercise%20above%20to%20be%20finished%20%7C%0A%20%20%20%20%7C%20*An%20ancestor%20raised%20an%20exception*%20%7C%20a%20cell%20this%20one%20needs%20failed%3A%20fix%20that%20cell%20first%2C%20and%20this%20one%20runs%20by%20itself%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b0bd732fcdb4e59512204d6ed18e0612