03 Python语句

white Python 2018-03-07 81 次浏览 03 Python语句已关闭评论

循环

死循环

while 1==1:
    print('ok')

‘!=’ 不等于符号

while 1!=1:
    print('ok')

while语句循环

count = 0
while count < 10 :
    print(count)
    count = count +1 

列出1、2、3、4、5、6、8、9、10

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

列出1到100之间的所有奇数

n = 1
while n < 101:
    if n % 2 == 0 :
        pass
    else:
        print(n)
    n = n +1 

列出1+2+3+…….+99的总和

n = 1 
s = 0 

while n < 100 :  
    n = n + 1
    s = s + n   
print(s)

列出1-2+3-4……99的总和

n = 1 
s = 0 
while n < 100 :
    if n%2 == 0:
        s = s-n
    else:
        s = s + n 
    n = n + 1
print(s)

While……else 循环

n = 0 
while n<10:
    print(n)
    n = n+1
else:
    print('end')

continue:终止当前循环,开始下一次循环

n = 0 
while n<10:
    if n == 7:
        n=n+1
        continue
    print(n)
    n = n+1 
print('end')

 break:终止所有循环

n = 0 
while n<10:
    if n == 7:
        n=n+1
        break
    print(n)
    n = n+1 
print('end')

用户登录三次机会

count = 0
while count<3:
    user = input('请输入用户名')
    pwd = input('请输入密码')
    if user == 'white' and pwd == '1234':
        print('欢迎进入python世界')
        break
    else:
        print('用户名或密码不正确')
    count = count + 1

 

in , not in语句

用于查询某东西是否在某个东西里面

name = '清风扬'
if '风' in name :
    print('ok')
else:
    print('no good')


运算符****

‘+’加,‘-’减,‘*’乘,‘/’除,‘**’幂,‘%’余‘//’商 in , not in

回顶部