博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 学习笔记【05】流程控制
阅读量:5875 次
发布时间:2019-06-19

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

  If...Else

1 _username = "A1_Code" 2 _password = "123" 3  4 username = input("username:") 5 password = input("password:") 6  7 if _username == username and _password == password: 8     print("Welcome user {name} login...".format(name=username)) 9 else:10     print("Invalid username or password!")

  If...Elif...Else

1 age_of_oldboy = 562 guess_age = int(input("guess age:"))3 4 if guess_age == age_of_oldboy:5     print("yes,you got it.")6 elif guess_age > age_of_oldboy:7     print("think smaller...")8 else:9     print("think bigger!")

   while

1 count = 02 while True:3     print("count:", count)4     count = count + 1 #count+=1

  Break 跳出循环

1 count = 02 while True:3     print("count:", count)4     count = count + 1 #count+=15     if count == 1000:6         break

  加计数器

1 age_of_oldboy = 56 2  3 count = 0 4 while True: 5     if count == 3: 6         break 7     guess_age = int(input("guess age:")) 8     if guess_age == age_of_oldboy: 9         print("yes,you got it.")10         break11     elif guess_age > age_of_oldboy:12         print("think smaller...")13     else:14         print("think bigger!")15 16     count += 1

 While 后加条件

1 age_of_oldboy = 56 2  3 count = 0 4 while count < 3: 5     if count == 3: 6         break 7     guess_age = int(input("guess age:")) 8     if guess_age == age_of_oldboy: 9         print("yes,you got it.")10         break11     elif guess_age > age_of_oldboy:12         print("think smaller...")13     else:14         print("think bigger!")15 16     count += 1

   While...Else

1 age_of_oldboy = 56 2  3 count = 0 4 while count < 3: 5     if count == 3: 6         break 7     guess_age = int(input("guess age:")) 8     if guess_age == age_of_oldboy: 9         print("yes,you got it.")10         break11     elif guess_age > age_of_oldboy:12         print("think smaller...")13     else:14         print("think bigger!")15     count += 116 else:17     print("you have tried too many times...fuck off")

   For

1 for i in range(10):2     print("loop", i)

  For...Else

1 age_of_oldboy = 56 2  3 for i in range(3): 4     guess_age = int(input("guess age:")) 5     if guess_age == age_of_oldboy: 6         print("yes,you got it.") 7         break 8     elif guess_age > age_of_oldboy: 9         print("think smaller...")10     else:11         print("think bigger!")12 else:13     print("you have tried too many times...fuck off")

   For 跳跃循环

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

  优化

1 age_of_oldboy = 56 2  3 count = 0 4 while count < 3: 5     if count == 3: 6         break 7     guess_age = int(input("guess age:")) 8     if guess_age == age_of_oldboy: 9         print("yes,you got it.")10         break11     elif guess_age > age_of_oldboy:12         print("think smaller...")13     else:14         print("think bigger!")15     count += 116     if count == 3:17         countine_confirm = input("Do you want to keep guessing..?")18         if countine_confirm != 'n':19             count = 0

  加断点 Debug

  Continue 跳出本次循环,继续下次循环;Break 结束循环

1 for i in range(0, 10):2     if i < 3:3         print("loop", i)4     else:5         continue6     print("hehe...")

   循环套循环

1 for i in range(10):2     print('----------', i)3     for j in range(10):4         print(j)5         if j > 5:6             break

 

转载于:https://www.cnblogs.com/a1-code/p/6000185.html

你可能感兴趣的文章
Linux下的搜索查找命令的详解(locate)
查看>>
福利丨所有AI安全的讲座里,这可能是最实用的一场
查看>>
开发完第一版前端性能监控系统后的总结(无代码)
查看>>
Python多版本情况下四种快速进入交互式命令行的操作技巧
查看>>
MySQL查询优化
查看>>
【Redis源码分析】如何在Redis中查找大key
查看>>
关于链接文件的探讨
查看>>
android app启动过程(转)
查看>>
Linux—源码包安装
查看>>
JDK8中ArrayList的工作原理剖析
查看>>
安装gulp及相关插件
查看>>
如何在Linux用chmod来修改所有子目录中的文件属性?
查看>>
Applet
查看>>
高并发环境下,Redisson实现redis分布式锁
查看>>
乌克兰基辅一世遗修道院起火 现场火光照亮夜空
查看>>
[iOS 10 day by day] Day 2:线程竞态检测工具 Thread Sanitizer
查看>>
Centos/Ubuntu下安装nodejs
查看>>
关于浏览器的cookie
查看>>
Hyper-V 2016 系列教程30 机房温度远程监控方案
查看>>
国内先进的智能移动广告聚合平台-KeyMob聚合
查看>>