博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day01-if else、while、for、continue、break
阅读量:6486 次
发布时间:2019-06-24

本文共 3973 字,大约阅读时间需要 13 分钟。

1.1  if else

猜测年龄:(一次结束)

1 age_of_zhou = 27 2  3 guess_age = int(input("guess age:")) 4  5 if guess_age == age_of_zhou: 6  7     print("yes,you got it.") 8  9 elif guess_age > age_of_zhou:10 11     print("think smaller...")12 13 else:14 15     print("think bigger!")

 

1.2  while循环

1 count = 02 3 while True:        #死循环4 5     print("count:",count)6 7     count +=1

 

猜测年龄(连续猜测,最多三次)

1 age_of_zhou = 27 2  3 count = 0 4  5 while True: 6  7     if count ==3:          #利用count计数器,实现循环次数限制 8  9         break10 11     guess_age = int(input("guess age:"))12 13     if guess_age == age_of_zhou:14 15        print("yes,you got it.")16 17        break18 19     elif guess_age > age_of_zhou:20 21          print("think smaller...")22 23     else:24 25           print("think bigger!")26 27     count +=1

 

代码优化:

1 age_of_zhou = 27 2  3 count = 0 4  5 while count <3:            #直接while count 6  7     guess_age = int(input("guess age:")) 8  9     if guess_age == age_of_zhou:10 11        print("yes,you got it.")12 13        break14 15     elif guess_age > age_of_zhou:16 17          print("think smaller...")18 19     else:20 21           print("think bigger!")22 23     count +=1

 

失败三次后,有提示

1 age_of_zhou = 27 2  3 count = 0 4  5 while count <3: 6  7     guess_age = int(input("guess age:")) 8  9     if guess_age == age_of_zhou:10 11        print("yes,you got it.")12 13        break14 15     elif guess_age > age_of_zhou:16 17          print("think smaller...")18 19     else:20 21           print("think bigger!")22 23     count +=124 25 if count ==3:26 27     print("you have tried too many times..fuck off")28 或者29 30 age_of_zhou = 2731 32 count = 033 34 while count <3:35 36     guess_age = int(input("guess age:"))37 38     if guess_age == age_of_zhou:39 40        print("yes,you got it.")41 42        break43 44     elif guess_age > age_of_zhou:45 46          print("think smaller...")47 48     else:49 50           print("think bigger!")51 52     count +=153 54 else:55 56     print("you have tried too many times..fuck off")

 

1.3  for循环

1 for i in range(10):      #从0开始打印,不包括最后一个数2 3     print ("loop",i)

 

 猜测年龄:

1 age_of_zhou = 27 2  3 for i in range(3): 4  5     guess_age = int(input("guess age:")) 6  7     if guess_age == age_of_zhou: 8  9        print("yes,you got it.")10 11        break12 13     elif guess_age > age_of_zhou:14 15          print("think smaller...")16 17     else:18 19           print("think bigger!")20 21 else:22 23     print("you have tried too many times..fuck off")

 

设置步长为2

for i in range(0,10,2):         #从0开始,不包括最后一个数     print ("loop",i)

 

 猜测年龄,提示还继续吗

1 age_of_zhou = 27 2  3 count = 0 4  5 while count <3: 6  7     guess_age = int(input("guess age:")) 8  9     if guess_age == age_of_zhou:10 11        print("yes,you got it.")12 13        break14 15     elif guess_age > age_of_zhou:16 17          print("think smaller...")18 19     else:20 21           print("think bigger!")22 23     count +=124 25     if count ==3:26 27         countinue_confirm = input("do you want to keep guessing? Y/N:")28 29         if countinue_confirm == "Y":30 31             count = 0         #count复位,重新计数 32 33         elif countinue_confirm =="N":34 35             break             #break结束整个循环
 

 

1.4  countinue、break、exit()

1、continue是结束本次循环跳到下一个循环

1 for i in range(0,10):2     j = int(input("input:"))3     if i==j:4         continue
5 else: 6 print(i,j) 7 else: 8 print("current") 9 print("hello")

 

2、break是结束当前循环层

 

1 for i in range(0,10):2     j = int(input("input:"))3     if i==j:4         break5     else:6         print(i,j)7 else:8     print("current")9 print("hello")

 

3、exit()退出当前整个程序

1 for i in range(0,10):2     j = int(input("input:"))3     if i==j:4         exit("退出程序")5     else:6         print(i,j)7 else:8     print("current")9 print("hello")

 

双重循环

1 for i in range(10): 2  3     print("----------",i) 4  5     for j in range(10): 6  7         print(j) 8  9         if j >5:10 11             break

 

转载于:https://www.cnblogs.com/Study-Blog/p/6478577.html

你可能感兴趣的文章
set集合
查看>>
SVN服务器的搭建和使用
查看>>
mvc中枚举的使用和绑定枚举值到DropDownListFor
查看>>
多目标跟踪的评价指标
查看>>
HTTPS(SSL)详解以及PHP调用方法
查看>>
突发小事件,USB接口问题
查看>>
Nginx负载均衡配置实例详解
查看>>
L1-009. N个数求和
查看>>
sqlserver 批量删除存储过程(转)
查看>>
自建型呼叫中心
查看>>
Inno setup中定制安装路径
查看>>
要懂得对你的老板好一点!
查看>>
visio如何让动态连接线的单箭头变成双箭头?
查看>>
poj 1273 Drainage Ditches 网络流最大流基础
查看>>
Bash: how to check if a process id (PID) exists
查看>>
Mirantis Fuel fundations
查看>>
启动Tomcat一闪而过——分析及解决过程
查看>>
Android intent action大全
查看>>
使用 Flash Builder 的 Apple iOS 开发过程
查看>>
RabbitMq_05_Topics
查看>>