python oneday

常量大写

用户交互数据类型是str

while循环,break跳出循环,continue终止循环。

练习题:

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

count=0

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

 

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

count=1
sum=0    
while count<=100:

sum=count+sum
count=count+1
print(sum)

 



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

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

i=1

while i<=100:
if i%2==1:
c=i
print('奇数',c)
else :
print('偶数',i)
i=i+1

 

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

i=1
j=2
count=0
while i<=98:
i=i+2
j=j+2
count=count+1
c=i-j
print(c*count)

 

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

admin='thx'
password='123'
i=1
while True:
name=input('请输入账户号')
passw=input('请输入密码')
if name==admin and passw==password:
print('正确')
break
else:
print('错误你还可以尝试',3-i,'')
i=i+1
if i>3:
break

 改进:

username='thx'
password='123'
i=0
while i<3:
    username=input('请输入账号')
    password=input('请输入密码')
    if username=='thx' and password=='123':
        print('输入正确')
        break
    else:
        print('你还有%d次机会'%(2-i))
        if i==2:
            x=input('你还想试试吗')
            if x=='yes':
                i=0
                continue
            else:
                print('再见')
                break
    i=i+1

 

posted @ 2018-10-04 14:47  我叫小明  阅读(180)  评论(0)    收藏  举报