Python学习笔记 | for循环
一、for循环
注意:
- for循环不需要额外变量进行计数
- range(10):代表可迭代的10个数:0~9
- range(10)的类型就是range型
二、局部变量和全局变量
三、Python代码
import math
def save_money_in_weeks(money_per_week, increase_money, total_week):
"""
计算n周存款金额
"""
money_list = []
for i in range(total_week):
money_list.append(money_per_week)
saving = math.fsum(money_list)
money_per_week += increase_money
return saving
def main():
"""
主函数
"""
money_per_week = float(input('请输入每周存款金额:'))
increase_money = float(input('请输入每周递增金额:'))
total_week = int(input('请输入总周数:'))
saving = save_money_in_weeks(money_per_week, increase_money, total_week)
print('总存款金额:', saving)
if __name__ == '__main__':
main()
结果展示: