while the usage of the python (digital sum between 0 ~ 100, while to achieve user login request)

A: while usage

"" "
The while condition ():
when the conditions are met, do 1 thing
when the conditions are met, do 2 things
...
" ""

#1.定义一个整数变量,记录循环的次数
i = 1
#2.开始循环
while i <= 3:
    #希望循环内执行的代码
    print('hello python')
    #处理计数器
    i += 1

Here Insert Picture Description
while infinite loop:

#定义死循环,会一直执行下去
while True:
    print('hello python')

Here Insert Picture Description

Example 1: summing the number between 0 and 100

#1.定义一个整数记录循环的次数
i = 0

#2.定义最终结果的变量
result = 0

#3.开始循环
while i <= 100:
     print(i)
#4.每次循环都让result和i这个计数器想加
    result += i
#5.处理计数器
     i += 1

print('0~100之间的数字求和结果为 %d' %result)

Here Insert Picture Description
Examples 2: while implementing user login requirements

trycount = 0

while trycount < 3:
    name = input('用户名:')
    passwd = input('密码:')
    if name == 'root' and passwd == 'westos':
        print('登录成功')
        break
    else:
        print('登录失败')
        print('您还剩余%d次机会' %(2 - trycount))
        trycount += 1
else:
    print('登录次数超过三次,请稍后登录')

Here Insert Picture Description

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/bmengmeng/article/details/94002589