为了账号安全,请及时绑定邮箱和手机立即绑定

仅在确定循环中打印最后一个值

仅在确定循环中打印最后一个值

大话西游666 2021-03-02 17:15:15
如何打印确定范围内的最后一个值?def main(): print "This program calculates the future value of a 10-year investment." principal = input("Enter the initial principle: ") apr = input("Enter the annual interest rate: ") for i in range(10):  principal = principal * (1 + apr)  print "The value in 10 years is:", principal 输出:The value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXXThe value in 10 years is XXXXXXX如何仅打印循环的最后一次迭代?
查看完整描述

3 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

取消最后一行的缩进(请不要再使用1个空格缩进,PEP-8建议使用4个空格)


def main():

    print "This program calculates the future value of a 10-year investment."


    principal = input("Enter the initial principle: ")

    apr = input("Enter the annual interest rate: ")


    for i in range(10):

        principal = principal * (1 + apr)

    print "The value in 10 years is:", principal 


查看完整回答
反对 回复 2021-03-31
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

只需将print行移出for-loop即可:


for i in range(10):

    principal = principal * (1 + apr)

print "The value in 10 years is:", principal 


查看完整回答
反对 回复 2021-03-31
?
PIPIONE

TA贡献1829条经验 获得超9个赞

  with open("story.txt",encoding="utf-8") as f:

        for line in f:

            for word in line.split()

                aList.append( word )

        print(aList)


查看完整回答
反对 回复 2021-03-31
  • 3 回答
  • 0 关注
  • 172 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号