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

#!/usr/bin/env python
#coding:utf-8
import getpass
import time
count=1
while count <=10:
    if count==7:
        print("")
    else:
        print(count)
    count=count+1

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

#!/usr/bin/env python
#coding:utf-8
import getpass
import time
count=1
sum=0
while count <=100:
    sum=sum+count
    count=count+1
print(sum)

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

#!/usr/bin/env python
#coding:utf-8
import getpass
import time
count=1
while count <=100:
    if(count%2!=0):
        print(count)
    count=count+1

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

#!/usr/bin/env python
#coding:utf-8
import getpass
import time
count=1
while count <=100:
    if(count%2==0):
        print(count)
    count=count+1

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

#!/usr/bin/env python
#coding:utf-8
import getpass
import time
count=1
sum=0
a=1
while a<=99:
    if a%2==0:
        sum=sum+count*(-1)
    else:
        sum=sum+count
    count=count+1
    a=a+1
print(sum)

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

#!/usr/bin/env python
#coding:utf-8
import getpass
import time
count=1
pwd=getpass.getpass("请输入密码:")
if pwd!="123456":
    while count<=3:
        count=count+1
        pwd=getpass.getpass("请重新输入:")
        if pwd!="123456":
            continue
        else:
            print("成功输入")
            break
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐