第二章 当型循环理财
代码:
import pdb money=50000 rate=[0.0325,0.03,0.03,0.02,0.0175] for i in rate: money=round(money+money*i,2) print("Five years later,your wealth is:",money,"yuan") money1=100000 year=0 while money1>0: money1=round(money1*(1+0.037),2)-20000 year+=1 pdb.set_trace() print(year,"year's later,wealth is taken out")
效果:
Five years later,your wealth is: 56841.97 yuan > /me/textbook/chapter/2/p37_2_3_wealth.py(10)<module>() -> while money1>0: (Pdb) p money1,year (83700.0, 1) (Pdb) c > /me/textbook/chapter/2/p37_2_3_wealth.py(10)<module>() -> while money1>0: (Pdb) p money1,year (66796.9, 2) (Pdb) c > /me/textbook/chapter/2/p37_2_3_wealth.py(10)<module>() -> while money1>0: (Pdb) p money1,year (49268.39, 3) (Pdb) c > /me/textbook/chapter/2/p37_2_3_wealth.py(10)<module>() -> while money1>0: (Pdb) p money1,year (31091.32, 4) (Pdb) c > /me/textbook/chapter/2/p37_2_3_wealth.py(10)<module>() -> while money1>0: (Pdb) p money1,year (12241.7, 5) (Pdb) c > /me/textbook/chapter/2/p37_2_3_wealth.py(10)<module>() -> while money1>0: (Pdb) p money1,year (-7305.360000000001, 6) (Pdb) c 6 year's later,wealth is taken out
总结:
- 两位小数round(i,2);
- 取完理财产品,循环条件为money1>o;
- import pdb,在循环体后面加入pdb_set_trace(),然后执行程序,在{pdb}提示符后面输入p,后面输入需要显示的变量,多个用逗号隔开,显示循环一次的变量值,然后按c继续循环。