当前位置:Gxlcms > Python > python学习日记(1)

python学习日记(1)

时间:2021-07-01 10:21:17 帮助过:42人阅读

之前学习了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...")


猜三次 猜三次没猜中 就退出 则要使用到循环


( 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


问: 该如何加?使初始游戏 能循环猜测?

改进2

number_of_kids =8

while True:
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...")


问题: 为什么while true 要放在这个位置?
答:while true 代表无限循环,不中断。若while true 放在下面一行的下面的话,
会无法循环下一个input,即会中断游戏。

运行结果:可无限次输入数字猜测 无限循环
缺点:当输入正确答案时仍然继续猜测,没有结束。

问:改进如何在猜对的时候 直接结束程序

改进3:

number_of_kids =8

while True:
guess_number=int(input("guess_number_of_kids:"))

if guess_number==number_of_kids:
print("You got it!")
break
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")


break的添加可以使得在猜对数字的时候 结束游戏。但若猜不对如何是之在
指定次数内结束游戏?

改进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_kids:
print("You got it!")
break
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

加了 if count ==3:break 后,还是没有在第三次猜测的时候成功。
原因: 没有加计数!

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_kids:
print("You got it!")
break
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")


count+=1


非常棒!



是否可以优化?

问:有什么可以代替 if count ==3: break 以及计数?

number_of_kids =8

count = 0


是否还可以优化?

有什么可以代替 if count ==3: break
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>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

count+=1

print("you have tried too many times..fuck off")

只有在第三次还没有猜对的情况下进行fuckoff
进行以下修改:

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>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

count+=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>number_of_kids:
print("think smaller...")
else:
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时,即进入第三次循环,再一次猜错时
出现bigger or smaller 后 走下一个程序,即count+=1等于3,进入else运行,后面紧跟fuckoff。

作业改动思路:要使的第三次出来的时候 只显示数字 和小了大了提示,不显示fuckoff,则说明当count+=1等于3时,
不直接走fuckoff的程序,则说明fuckoff的设置不能出现在前面while count <3 的子程序中,而是应该并列程序,
当count=3时,继续出现猜数字的界面,后不管猜错猜对都fuckoff,并且结束。

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>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

count+=1

if count ==3:
guess_number =int(input("guess_number_of_kids:"))
print("you have tried too many times..fuck off")
break

while优化课

如何用for来代替 while


number_of_kids =8

count = 0

while count <3: #for i in range(3)

guess_number=int(input("guess_number_of_kids:"))

if guess_number==number_of_kids:
print("You got it!")
break
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

count+=1

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:
print("You got it!")
break
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

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!")
break
elif guess_number> number_of_kids:
print("think smaller...")
else:
print("think bigger...")

count+=1
if count==3:
countine_confirm = input("do you want keeping guessing...?")
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...

后来加断点???啥意思?
for i in range(0,10):
if i<3:
print("loop",i)
else:
continue(作用是跳出本次循环,进入下一次循环)
print("hehe"..)
问: 同样代码 却没有hehe?


新知识点:双循环

for i in range(10):
print("________",i)
for j in range(10):
print(j)

执行结果:
=== RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35-32/4.py ===
________ 0

以上就是python学习日记(1)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行