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

python基础练习题

程序员文章站 2024-02-24 13:40:25
...

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

count = 1
while count< 11:
    if count == 7:   
        count+=1
        continue
    print(count)
    count+=1

 

2、求1-100的所有数的和

counts = 1
num = 0
while counts <101:
    num +=counts
    counts+=1
print(num)

  

3、输出 1-100 内的所有奇数

counte = 1
while counte < 101:
    if counte % 2 == 1:
        print(counte)
    counte+=1

  

4、输出 1-100 内的所有偶数

counte = 1
while counte < 101:
    if counte % 2 != 1:
        print(counte)
    counte+=1

  

5、求1-2+3-4+5 ... 99的所有数的和

counte = 1
num = 0
num1 = 0
while counte < 100:
    if counte % 2 != 1:
        num += -counte
    else:
        num1 += counte
    counte+=1
print(num+num1)

  

6、用户登陆(三次机会重试)

count = 0
while count<3:
    count +=1
    username = input("username:")
    password = input("password:")
    if username == 'alex' and password == '123456':
        print('登陆成功!')
        break
    else:
        print('请重试!')