接續上次的代碼,是不是只有執行一次才就結束,想要再繼續猜,就要在執行一次,是不是有點挺麻煩的?

所以這次我們就來再多做一點點功能進去,讓代碼可以多次循環地執行代碼,Go....

首先,我們先來了解一下,簡單的 while循環 怎麼寫

#!/usr/bin/env python3
# -*- coding:utf-8 -*- count = 0 while True:
print("count:", count)
count += 1 ---------------執行結果--------------- count: 70937
count: 70938
count: 70939
count: 70940
count: 70941
count: 70942
count: 70943
count: 70944

上面的代碼是一個簡單的 while循環 ,這是一個無限的迴圈,只要while為真,就會一直執行不會中斷,需要由使用者自行中斷,

簡單的會了,我們開始來結合上次的代碼

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 while True:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...") ---------------執行結果--------------- guess age:23
You may think bigger...
guess age:98
You may think smaller...
guess age:84
You may think smaller...
guess age:35
Bingo, You got it!!!
guess age:

咦~有發現什麼不尋常的地方嗎?沒錯,即使你輸入正確了,這遊戲也不會結束,所以我們要在新增一個可以結束遊戲的條件,

要怎麼結束呢? 這時候我們必須在代碼中加入 break 就可以了,再修改一下代碼

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 while True:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...") ---------------執行結果--------------- guess age:39
You may think smaller...
guess age:28
You may think bigger...
guess age:59
You may think smaller...
guess age:39
You may think smaller...
guess age:35
Bingo, You got it!!! Process finished with exit code 0

這樣又達成目的了,可是我們想要限制這個遊戲猜只能猜三次,三次猜不中就退出遊戲,該怎麼寫呢?

這時候就需要一個計數器來協助完成這次的任務,所以再為代碼稍稍加強一下功能

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 count = 0 while True:
if count >= 3:
break
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1 ---------------執行結果--------------- # 故意猜超過三次,看看結果
guess age:1
You may think bigger...
guess age:1
You may think bigger...
guess age:1
You may think bigger... Process finished with exit code 0 # 先試試在三次內打對密碼,看看結果
guess age:3
You may think bigger...
guess age:36
You may think smaller...
guess age:35
Bingo, You got it!!! Process finished with exit code 0

hmm...上面代碼成功達成條件了,接下來我們要來想想,怎麼優化這個代碼?

上面代碼是不是可以把 if count >= 3: 放進 while 裡,修改一下代碼

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 count = 0 while count <= 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1 ---------------執行結果--------------- # 故意猜超過三次,看看結果
guess age:1
You may think bigger...
guess age:69
You may think smaller...
guess age:34
You may think bigger... Process finished with exit code 0 # 先試試在三次內打對密碼,看看結果
guess age:32
You may think bigger...
guess age:35
Bingo, You got it!!! Process finished with exit code 0

while count < 3 就會變成有條件的,當count 小於 3就執行,如果大於3,就不執行。

但還是有稍微美中不足的地方,就是輸入超過三次,並沒有打印出任何訊息,會讓人感覺有點莫名其妙,

所以再修改一下代碼

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 count = 0 while count <= 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1 print("You have tried too many, exit") ---------------執行結果---------------
# 故意猜超過三次,看看結果
guess age:3
You may think bigger...
guess age:3
You may think bigger...
guess age:3
You may think bigger...
You have tried too many, exit Process finished with exit code 0 # 先試試在三次內打對密碼,看看結果
guess age:4
You may think bigger...
guess age:35
Bingo, You got it!!!
You have tried too many, exit Process finished with exit code 0

咦~怎麼打對密碼也顯示 You have tried too many, exit,看來還做得不夠好,再修改一下代碼

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 count = 0 while count <= 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1 if count == 3:
print("You have tried too many, exit") ---------------執行結果---------------
# 故意猜超過三次,看看結果
guess age:1
You may think bigger...
guess age:2
You may think bigger...
guess age:3
You may think bigger...
You have tried too many, exit Process finished with exit code 0 # 先試試在三次內打對密碼,看看結果
guess age:15
You may think bigger...
guess age:35
Bingo, You got it!!! Process finished with exit code 0

呼,這次終於都對了…

可是我們還有另一種更牛逼的寫法,是 Python 特有的寫法

就讓我們最後一次修改代碼,讓它看起來更牛逼點!!!

#!/usr/bin/env python3
# -*- coding:utf-8 -*- age_of_ironman = 35 count = 0 while count <= 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_ironman:
print("Bingo, You got it!!!")
break
elif guess_age > age_of_ironman:
print("You may think smaller...")
else:
print("You may think bigger...")
count += 1
else:
print("You have tried too many, exit") ---------------執行結果---------------
# 故意猜超過三次,看看結果
guess age:4
You may think bigger...
guess age:5
You may think bigger...
guess age:6
You may think bigger...
You have tried too many, exit Process finished with exit code 0 # 先試試在三次內打對密碼,看看結果
guess age:7
You may think bigger...
guess age:67
You may think smaller...
guess age:35
Bingo, You got it!!! Process finished with exit code 0

終於完成了,但這個是怎麼判斷的呢?請看下圖

Python 基礎 - while流程判斷的更多相关文章

  1. Python 基礎 - for流程判斷

    今天介紹另一個循環判斷式 for循環,首先,先寫一個很簡單的 for循環的代碼 #!/usr/bin/env python3 # -*- coding:utf-8 -*- for i in range ...

  2. Python 基礎 - if else流程判斷

    hmm~前面講了那麼多,終於可以稍稍的正式進入另一個階段,沒錯,要開始寫判斷式了 這次先從最簡單的判斷式開始,if else 開始- Go 首先,之前有寫有一個簡單的互動式 用戶輸入 的代碼,忘記了嗎 ...

  3. Python 基礎 - pyc 是什麼

    Python2.7 版中,只要執行 .py 的檔案後,即會馬上產生一個 .pyc 的檔案,而在 Python3 版中,執行 .py 的檔案後,即會產生一個叫 __pycache__ 的目錄,裡面也會有 ...

  4. Python 基礎 - 文件操作_v2

    嗯,那如何要把游標的位置給打印來? #!/usr/bin/env python3 # -*- coding:utf-8 -*- f = open('test', 'r') print(f.tell() ...

  5. Python 基礎 - 字典的操作使用

    接下來介紹字典,這在未來工作上,會是很常使用的,就來好好了解一下唄- 字典是一個 key(鍵)-value(值) 的數據類型,可以儲存很多訊息 #!/usr/bin/env python3 # -*- ...

  6. Python 基礎 - 字符串常用操作

    字符串常用操作 今天就介紹一下常用的字符串操作,都是以 Python3撰寫的 首字母變大寫 #!/usr/bin/env python3 # -*- coding:utf-8 -*- name = & ...

  7. GO語言基礎教程:流程控制

    在開始一個新的章節之前先來回顧上一篇文章的部份,首先我們來看這段代碼: package main import ( "fmt" ) func main(){ var x,y int ...

  8. Python 基礎 - 集合的使用

    集合是一個無序的,不重複的數據組合,主要的作用如下 去重,把一個列表變成集合,就會自動去重了. 關係測試,測試二組數據之前的交集.差集.聯集等關係. 接下來我們來實作看看什麼是去重 #!/usr/bi ...

  9. python基礎學習第二天

    字符编码 # 需知:## 1.在python2默认编码是ASCII, python3里默认是unicode## 2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf ...

随机推荐

  1. Xcode7中,如何新建category分类

    易忘,所以留存: 1, 2, 3, 结果如下: 补充: http://tech.meituan.com/DiveIntoCategory.html

  2. Java-简陋的图书管理

    本文代码为原创一个简陋的管理系统,只做功能的测试.并没有去完善所有应有的功能,只做了输入输出查找.仅供参考! 菜单部分: import java.util.Scanner; public class ...

  3. 安装DotNetCore.1.0.0-VS2015Tools.Preview2失败解决方案

    1.把安装文件放入非系统盘 2.命令行带参数运行: DotNetCore.1.0.0-VS2015Tools.Preview2.0.1.exe SKIP_VSU_CHECK=1 或 DotNetCor ...

  4. 使用XML定制Ribbon的一点小前奏(稍微再进一步的理解XML)

    定制文档级Ribbon界面的实现思路: 1.excel的文件使用rar+xml的形式保存在本地. 2.用压缩软件打开文件,以规范的格式直接编缉或添加xml文件 3.使用excel文件时,主程序会解析x ...

  5. php: 学习记录

    1.get_object_vars($obj) 获取对象$obj的属性数组 2.类和对象 <?php // 类和对象 echo "类和对象" . "\n" ...

  6. 《BI那点儿事》数据流转换——模糊分组转换

    在模糊查找中我们提到脏数据是怎样进入到表中的事情,主要还是由于一些“Lazy-add”造成的.这种情况我们的肉眼很容易被欺骗,看上去是同一个单词,其实就差那么一个字母,变成了两个不同的单词.一个简单的 ...

  7. bootloader(转)

    本文详细地介绍了基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念.软件设计的主要任务以及结构框架等内容. 1. 引言在专用的嵌入式板子运行 GNU/Linux 系统已经变得 ...

  8. cellery ImportError & AttributeError

    一.zz的问题 celery 运行work要进入到 文件所在的文件夹下执行 二.AttributeError: 'Flask' object has no attribute 'user_option ...

  9. 如何搭建一个linux服务器

    1, 首先 下载一个linux server 系统镜像 ubuntu 64bit下载 http://www.ubuntu.com/download/server/thank-you/?version= ...

  10. LinkedList其实就那么一回事儿之源码分析

    上篇文章<ArrayList其实就那么一回儿事儿之源码分析>,给大家谈了ArrayList, 那么本次,就给大家一起看看同为List 家族的LinkedList. 下面就直接看源码吧: p ...