欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python循环判断基础学习

程序员文章站 2022-07-14 23:10:29
...

使用while循环输入1 2 3 4 5 6  8 9 10

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

 

求1-100所有数的和

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

输出1-100所有偶数

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

求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 判断三次登录密码错误

方法一:

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("三次机会错误请重试")

方法二:

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("三次错误请重试")