Python print 输出到文件
Python3.x
#!/usr/bin/env python3 #coding:utf-8 year = 1 years = 5 bj = 10000 rate = 0.05 f = open("./source/interest.bak", "w+") while year < years: bj = bj * (1 + rate) print("第{0}年,本金息总计{1}".format(year, bj), file=f) year += 1
Python2.x
#!/usr/bin/env python #coding:utf-8 year = 1 years = 15 bj = 10000 rate = 0.05 f = open("./source/interest.bak", "w+") while year < years: bj = bj * (1 + rate) print >> f, "第%d年,本金息总计%0.2f" % (year, bj) year += 1
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。