Python 基礎 - while流程判斷
接續上次的代碼,是不是只有執行一次才就結束,想要再繼續猜,就要在執行一次,是不是有點挺麻煩的?
所以這次我們就來再多做一點點功能進去,讓代碼可以多次循環地執行代碼,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流程判斷的更多相关文章
- Python 基礎 - for流程判斷
今天介紹另一個循環判斷式 for循環,首先,先寫一個很簡單的 for循環的代碼 #!/usr/bin/env python3 # -*- coding:utf-8 -*- for i in range ...
- Python 基礎 - if else流程判斷
hmm~前面講了那麼多,終於可以稍稍的正式進入另一個階段,沒錯,要開始寫判斷式了 這次先從最簡單的判斷式開始,if else 開始- Go 首先,之前有寫有一個簡單的互動式 用戶輸入 的代碼,忘記了嗎 ...
- Python 基礎 - pyc 是什麼
Python2.7 版中,只要執行 .py 的檔案後,即會馬上產生一個 .pyc 的檔案,而在 Python3 版中,執行 .py 的檔案後,即會產生一個叫 __pycache__ 的目錄,裡面也會有 ...
- Python 基礎 - 文件操作_v2
嗯,那如何要把游標的位置給打印來? #!/usr/bin/env python3 # -*- coding:utf-8 -*- f = open('test', 'r') print(f.tell() ...
- Python 基礎 - 字典的操作使用
接下來介紹字典,這在未來工作上,會是很常使用的,就來好好了解一下唄- 字典是一個 key(鍵)-value(值) 的數據類型,可以儲存很多訊息 #!/usr/bin/env python3 # -*- ...
- Python 基礎 - 字符串常用操作
字符串常用操作 今天就介紹一下常用的字符串操作,都是以 Python3撰寫的 首字母變大寫 #!/usr/bin/env python3 # -*- coding:utf-8 -*- name = & ...
- GO語言基礎教程:流程控制
在開始一個新的章節之前先來回顧上一篇文章的部份,首先我們來看這段代碼: package main import ( "fmt" ) func main(){ var x,y int ...
- Python 基礎 - 集合的使用
集合是一個無序的,不重複的數據組合,主要的作用如下 去重,把一個列表變成集合,就會自動去重了. 關係測試,測試二組數據之前的交集.差集.聯集等關係. 接下來我們來實作看看什麼是去重 #!/usr/bi ...
- python基礎學習第二天
字符编码 # 需知:## 1.在python2默认编码是ASCII, python3里默认是unicode## 2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf ...
随机推荐
- linux 如何开机自动运行sh脚本
vi /etc/rc.d/rc.local #自动启动oracleecho 502 >/proc/sys/vm/hugetlb_shm_group su - oracle -c 'sh /dat ...
- Displaying Window In Center In Oracle Forms 6i
Center window automatically in Oracle Forms 6i, use the following procedure by passing window name ...
- chrome的timeline中stalled问题解析
原文地址 :http://foio.github.io/chrome-stalled/ 在公司国做一个运营活动,上线后PM总是抱怨访问速度过慢,影响运营效果.然而从前端的角度来说我已经做了如下优化: ...
- Linux IO实时监控iostat命令详解
简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...
- [分享] 晒一晒我的Windows7_SP1封装母盘(多图,附部分工具),老鸟飘过~
[分享] 晒一晒我的Windows7_SP1封装母盘(多图,附部分工具),老鸟飘过~ 大宝贝1 发表于 2012-8-9 18:01:57 https://www.itsk.com/thread-20 ...
- 运行js提示库没有注册错误8002801d的解决办法
运行js提示库没有注册错误8002801d的解决办法这个错误主要是因为服务器上的windows scripts版本较低,请按下面的链接下载较高版本windows scripts 5.6并在服务器上进行 ...
- 使用 GPG 对数据进行加密解密签名
一:使用 GPG 对数据进行加密解密签名 基本的工具使用 1. GPG 是GNUPG 免费开源的gpg加密工具,和同pgp兼容,pgp收费. 2. 在mac上使用https://gpgtools.or ...
- Excel 2013中单元格添加下拉列表的方法
使用Excel录入数据的时候我们通常使用下拉列表来限定输入的数据,这样录入数据就很少发生错误了.Excel 2013较以前的版本发生了很大的变化,那么在Excel 2013是如何添加下拉列表的呢? 下 ...
- Sprint2-3.0
后续安排 第16周 周二晚7点之前将本代码上传到GITHUB. GITHUB地址:https://github.com/QueenIcey/teamwork/tree/master/eslife1 周 ...
- vs---错误收集并自己解决后归纳
1.C++编译时,出现这样的错误 d:\program files\microsoft visual studio\vc98\include\stdio.h(36) : error C2143: sy ...