Python loop judgment basic learning

Use the while loop to input 1 2 3 4 5 6 8 9 10

a = 1
while a < 11:
    if a == 7:
        pass
    else:
        print(a)
    a = a + 1

 

Find the sum of all numbers 1-100

a = 1
sum = 0
while a < 101:
    sum = sum + a
    a = a + 1
print(sum)

Output 1-100 all even numbers

a = 1
while a < 101:
    temp = a % 2
    if temp == 0:
        print(a)
    else:
        pass

Find the sum of all trees in 1-2+3-4+5...99

a = 1
sum = 0
while a < 100:
    temp = a % 2
    if temp == 0:
        sum = sum - a
    else:
        sum = sum + a
    a = a + 1
print(sum)

Python judges three login password errors

method one:

passwd = "abcd1234"
huoqu = input("请输入核验身份密码:")
if huoqu == passwd:
    print("密码正确")
else:
    print("密码错误")
    huoqu = input("请重新输入:")
    if huoqu == passwd:
        print("密码正确")
    else:
        print("密码错误")
        huoqu = input("请重新输入:")
        if huoqu == passwd:
            print("密码正确")
        else:
            print("三次机会错误请重试")

Method Two:

passwd = "abcd1234"
huoqu = input("请输入核验身份密码:")
srcs = 1

while srcs < 3:
    if huoqu == passwd:
        print("密码正确")
        srcs = 3
    else:
        print("密码错误")
        huoqu = input("请重新输入:")
        srcs = srcs + 1
        if srcs == 3:
            print("三次错误请重试")

 

Guess you like

Origin blog.csdn.net/qq_15057857/article/details/104444187