pthon3学习之if,for,while循环

if循环

if语句

age = 32
_age =  int(input("age is:"))
if _age == age:
    print("OK")
elif _age > age:
    print("think smaller")
else:
    print("think bigger")
View Code

if语句猜测用户名密码

_username = '111111'
_password = '111111'
username =  input("username:")
password =  input("password:")
if _username == username and _password == password:
    print("welcome {user} login...".format(user=username))
else:
    print("wrong")
View Code

 for语句格式

for i in range(10):
    print("loop",i)
View Code

for语句,隔一个数打印一个

for i in range(0,10,2):
    print("loop",i)
View Code

for语句,猜测3次年龄错误退出

age = 32
for i in range(3):
  _age =  int(input("age is:"))
  if _age == age:
      print("OK")
      break
  elif _age > age:
      print("think smaller")
  else:
      print("think bigger")
else:
    print("you have tried too many times..")
View Code

while 循环,死循环

count = 0
while True:
    print("count:",count)
    count = count + 1
View Code

while循环,猜年龄错误3次,退出循环

age = 32
count = 0
while count < 3:
  _age =  int(input("age is:"))
  if _age == age:
      print("OK")
      break
  elif _age > age:
      print("think smaller")
  else:
      print("think bigger")
      count = count + 1
else:
    print("you have tried too many times..")
View Code

while循环,猜年龄错误3次,提示是否继续,如果输入N退出循环,如果不是N,继续循环3次。

age = 32
count = 0
while count < 3:
  _age =  int(input("age is:"))
  if _age == age:
      print("OK")
      break
  elif _age > age:
      print("think smaller")
  else:
      print("think bigger")
      count = count + 1
      if count == 3:
          sure = input("do you want continue?")
          if sure != 'n':
              count == 0
else:
    print("you have tried too many times..")
View Code

 while循环,循环打印1-100,第50次不打印,第60-80次,打印对应值的平方

count = 0
while count <= 100:
    if count == 50:
        pass
    elif count >= 60 and count <= 80:
        print(count*count)
    else:
        print("loop",count)
    count += 1
print("---loop is ended---")
View Code

while 循环,打印0-100内的偶数。

count = 0
while count <=100:
    if count % 2 == 0:
        print("loop",count)
    count = count + 1

 

posted on 2017-12-18 17:32  Stary-liuy  阅读(108)  评论(0)    收藏  举报

导航