如何在python的while循环内仅打印一次

时间:2020-06-30 09:38:31

标签: python

我只想在while循环内打印一次。如下图所示:

break

现在,我不想使用condition = True@app.route('/<string:charity_id>/glc') # @roles_accepted('admin',charity_id[:3]) def GLC(charity_id): ... ... return render_template('one.html',charity_id=charity_id) 完全停止循环。我只希望循环重新开始。 如果您需要任何澄清,请随时询问

3 个答案:

答案 0 :(得分:2)

要在一个无限的while循环中仅执行一次代码块,请使用下面的示例中的标志:

printed_flag = False
while True:
    a = int(input('enter a number'))
    b = int(input('enter another number'))

    if printed_flag:
        if a>b:
            print('a is bigger then b')
        else:
            print('b is bigger than a')
        printed_flag = False

答案 1 :(得分:1)

您可以添加计数器以重复循环,直到计数器停止。下面所做的是将计数器设置为无穷大。

i = 0
while i <= float("inf"):
    i += 1
    a = int(input('enter a number'))
    b = int(input('enter another number'))

    if a>b:
        print('a is bigger then b')
    else:
        print('b is bigger than a')

答案 2 :(得分:1)

使用可连续输入值且仅打印一次的计数器。

i=1
while True:

     # Asking for input(a and b)
     a = int(input('enter a number'))
     b = int(input('enter another number'))
     if i==1:
        i=i+1
        if a>b:
             print('a is bigger then b')
        else:
             print('b is bigger than a')
     else:
         continue