之前學習了Alex的python教學Alex的教學真的非常棒,也很幽默。嘿嘿。希望自己可以堅持下去。
把之前學的基礎知識整理回顧了一下。希望自己可以在學習python的路上越走越遠,大家互勉奧。
第一週複習總結
1 初始遊戲程式碼
number_of_kids =8
guess_number=int(input("guess_number_of_kids:"))
if guess_number==number_of_kids:
print("You got it!")
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")##else:
( while初相識
# 程式碼1:
count = 0
while True:
print("count:",count)
count = count +1 #count +=1
簡單舉例while
count = 0
while True:
print("count:",count)
count = count *2 #count*=2
if count == 1000:
break
問: 此如何加?使初始遊戲 能循環猜測?
while 真:
#if guess_number= =number_of_kids:
print("You got it!")
elif guess_number>number_of_kids:
1print("think printsmal("them)o.. bigger. ..")
問題: 為什麼while true 要放在這個位置?
答:while true 代表無限循環,不中斷。若while true 放在下面一行的下面的話,
會無法循環下一個input,也就是會中斷遊戲。
缺點:當輸入正確答案時仍持續猜測,沒有結束。
問:改進如何在猜對的時候直接結束程式
guess_number= int(input("guess_number_of_kids:"))
print("You got it!")
break##if print ("think smaller...")
else:
print("think bigger...")
print("think bigger...")
# 的新增位置中讓遊戲結束遊戲。但若猜不對如何是之在
指定次數內結束遊戲?
改進4:
number_of_kids =8
count = 0
while True:
if count ==3:
break
guess_number=int(input("guess_number_of_kids:"))
if guess_number==number_of_ki :You# print break print( # elif guess_number>number_of_kids:
print("think smaller...")
else:
# print("th)# bigprint("th)#ger") :break 之後,還是沒有在第三次猜測的時候成功。
原因: 沒有加計數!
number_of_kids =8
while True:
if count ==3:
break guess_number =int(input("guess_number_of_kids:"))if guess_number==number_of_kids: print("You got it!")
break##if print("think smaller...")
else:
print("think bigger...")
count+=1
棒!
是否可以最佳化?
問:有什麼可以代替 if count ==3: break 以及計數?
number_of_kids =8
count = 0
while True:
if count <3:
guess_number=int(input("guess_number_of_kids: "))
if guess_number==number_of_kids:
print("You got it!")
break
elif guess_number>n break
elif guess_number>number_of_gt;## elif guess_number>number_print_gt;#number; else :
print("think bigger...")
count+=1
print("you have tried too many times..fuck off")
進行以下修改:
guess_number=int(input("guess_number_of_kids:"))
elif guess_number>number_of_kids:
print("think smaller...")
. 1
if count ==3:
print("you have tried too many times..fuck off")
# 改進:進階一點哈哈哈哈
number_of_kids =8
count = 0
while count <3:
guess_number=int(input("guess_number_of_kids: "))
if guess_number==number_of_kids:
print("You got it!") break
elif guess_number> _ break
")
print("think bigger...")
count+=1
else:
print("you have tried too many times. .fuck off")
小傻作業: 4/9 週日
如何變動程式碼,使得第三次出來的時候只顯示數字,不顯示fuckoff ,等第四次輸入的時候,不提示大小,反而直接輸出fuckoff?
首先,理清上一段程式碼的想法:count從0開始運行 進入while循環,但凡在小於3的範圍內 只要猜對 就break。
沒猜對 就出現smaller or bigger。當count+=1等於2時,即進入第三次循環,再一次猜錯時
作業改變思路:要使的第三次出來的時候只顯示數字和小了大了提示,不顯示fuckoff,則說明當count+=1等於3時,
不直接走fuckoff的程序,則說明fuckoff的設定不能出現在前面while count <3 的子程序中,而是應該並列程序,
當count=3時,繼續出現猜數字的介面,後不管猜錯猜對都fuckoff,並且結束。
number_of_kids =8
while True:
if count <3:
guess_number =int(input ("guess_number_of_kids:"))if guess_number==number_of_kids: print("You it kids:
print("think smaller...")
print("think bigger...")
##count+=1
print("you have tried too many times..fuck off")
break
#
#
如何用for來代替while
while count <3: #for i in range(3)
guess_number=int(input("guess_number_of_kids:"))
if guess_number==number_of_kids:
print("# got itbreak")# guess_number>number_of_kids:print("think smaller...")
else:# print("think bigger...")
countprint("think bigger. else:
print("you have tried too many times..fuck off")
改成:
#number_of_kids =8
## count = 0
#for i in range(3)
guess_number=int(input("guess_number_of_kids:"))
if guess_number==number_of_kids:
break
elif guess_number>number_of_kids: print( ")
count+=1
else:
print("you have tried too many times..fuck off")
最基本的for迴圈。
for i in range(0,10,1): #1是預設值,可以不寫,不寫即代表1.
print("loop",i)
#for i in range(0,10,2):
print("loop",i)
運作結果:
=== RESTART: C:/Users/dell/AppData/ Local/Programs/Python/Python35-32/12.py ===
loop 0
loop 2
loop 4
loop 6
loop 8
>>>
for i in range(0,10,3):
print("loop",i)
運作結果:
=== RESTART: C:/Users /dell/AppData/Local/Programs/Python/Python35-32/12.py ===
=== RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35-32/12. py ===
loop 0
loop 3
loop 6
loop 9
>>>
再次優化猜數字遊戲:猜三次就退出貌似很決絕,是不是可以依照個人需求設定提示?
加:問還要不要玩?
number_of_kids=8
count=0
while count <3:
guess_number = int(input("guess_number_of_kids:"))
if guess_number == number_of_kids:
print("yep,you got it!")
umber_bumber _ _bumber # print("think smaller...")
else :
print("think bigger...")
count+=1
)
if countine_confirm !="n": 若是按下n則表示退出遊戲
count =0 當按下回車鍵後表示想要按回車鍵後表示必須繼續執行。
task: 單獨打出這段程式碼,理清思緒。
新知識點: continue
for i in range(0,10):
if i<3: print("loop",i)
else :
continue
print("hehe"..)
執行結果:=== RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35- 32/4.py ===
loop 0
hehe...
loop 1
hehe...
loop 2
hehe...
後來加斷點? ? ?啥意思?
if i<3:
print("loop",i)
else:
continue(作用是跳出本次循環,進入下一次迴圈)
print("hehe"..)
問: 同樣程式碼卻沒有hehe?
for i in range(10):
for j in range(10) :
print(j)
執行結果:
=== RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35-32/4.py ===
________ 0
以上是python學習日記(1)的詳細內容。更多資訊請關注PHP中文網其他相關文章!