Python( 七)用户输入和while循环

1、用户输入:
Input(“xxxx”)
Raw_input(“xxxxx”)

2、求模(求余)运算:%
4%3 ==1

#coding:utf-8
number = input("please input a number:\n")
if number == 0:
    print "请输入大于0的数字"
elif number % 2 ==0:
    print u"该数字是偶数"
else:
    print u"该数是奇数"

While循环

1、使用flag标识

print '--------------------------------'
print '\n demo2'
message =""
while message !="quit":
    message = raw_input("please input something\n")
    if message != "quit":
        print message

print '--------------------------------'
print 'demo3\n'
active = True
while active:
    infomation= raw_input("please input something\n")
    if infomation == "quit":
        active = False
    else:
        print infomation

2、break循环中断
以 while True开头的会一直无限循环下去,除非遇见break
注意:Break直接退出当前循环,不执行循环内剩下的代码

for i in range(0,10):
   while i == 5:
       print i
       print '已到达5'
       break
       print "test"
   print i

3、Continue
break是结束整个循环体,continue是结束单次循环

你可能感兴趣的:(Python( 七)用户输入和while循环)