python第一课练习题

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

num = 1

while num < 7:
    print(num)
    num = num + 1

while num < 10:
    num = num + 1
    print(num)

 

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

num = 1
count = 0

while num < 101:
    count = num + count
    num = num + 1

print(count)

 

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

num = 1

while num < 101:
    a = num % 2
    if a != 0:
        print(num, "是奇数")
    else:
        pass
        # print(num, "是偶数")
    num = num + 1

 

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

num = 1

while num < 101:
    a = num % 2
    if a != 0:
        pass
        # print(num, "是奇数")
    else:
        print(num, "是偶数")
    num = num + 1        

 

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

num = 1
count = 0

while num < 100:
    count = num + count
    num = num + 1

print(count)

 

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

name = str(123)
num = 0
a = input("请输入用户名:")

while num < 3:
    if name == a :
        print('登录成功')
        num = 3
    else:
        print('登录失败')
    num = num + 1
    if num < 3:
        a = input("请输入用户名:")

 

posted @ 2018-06-13 18:51  Trunkslisa  阅读(189)  评论(0编辑  收藏  举报