【问题标题】:How to keep plus number in loop如何保持加号循环
【发布时间】:2021-02-02 19:41:50
【问题描述】:

我想继续在非常循环中添加数字,或者有时很难解释,比如代码从顶部一直到末尾,而我的 plus 变量也加上从 1 到更高的数字。

]while True:
    plus = 0
    name = input('Name:')
    print(name)
    plus += 1
    print(plus)

【问题讨论】:

    标签: python-3.x loops numbers


    【解决方案1】:

    一般说明:

    • 首先,请记住缩进。循环体必须缩进 4 个空格 (编辑:当然,它可以是任意数量的空格,只要它比块级别更高)。
    • 其次,如果您将 while 循环条件设置为 True,则循环将永远运行,我认为这不是您的目标。如果您真的想在开始时保持“真”条件,请记住始终在循环内设置“中断”条件。

    我不知道我是否正确理解了您的问题-您的意思是,您想要输入顺序字符串值(名称),在循环中打印该名称,然后打印现在是什么时间-我说的对吗?

    那么解决方法是:

    plus = 0  # start value must be before the loop, in your example you set "plus" variable on 0 at the begining of each iteration
    
    
    while True:
    
        plus += 1
    
        name = input('Name:')
        print(name)
    
        print(plus)
        if plus >= 20 or name == 'Caroline':  # here write your contition to preserve the loop from infinite execution
            break
    

    【讨论】:

    • "循环体必须缩进 4 个空格。"不对。它可以缩进任意数量的空格,只要该数字比块高一级即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多