python学习日记(1)

巴扎黑
发布: 2017-06-23 10:57:51
原创
1576人浏览过

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


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

 

立即学习Python免费学习笔记(深入)”;


(  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
   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")

 

立即学习Python免费学习笔记(深入)”;

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

number_of_kids =8

count = 0

while count
   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

白月工作室培训学校学员(会员)管理系统1.01 GBK
白月工作室培训学校学员(会员)管理系统1.01 GBK

培训学校管理系统适合于目前的一般培训学校,比如英语、计算机、少儿培训中心等小型培训机构。本系统原本是针对计算机培训所设计。培训学校管理系统的大体功能为:一、 学员信息管理:1. 学员信息管理(会员形式)2. 操作员流水记录二、 学员报名管理1. 学员报名处理学员信息录入以意向性列表方式记录,以后可以跟踪2. 学员再次报名(二次或者多次学习其他项目)3. 学员退学处理4. 学员暂时保留学习处理(类似

白月工作室培训学校学员(会员)管理系统1.01 GBK 0
查看详情 白月工作室培训学校学员(会员)管理系统1.01 GBK

count = 0

while count
   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 当count=3时,继续出现猜数字的界面,后不管猜错猜对都fuckoff,并且结束。

number_of_kids =8

count = 0


while True:

if count        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

 

立即学习Python免费学习笔记(深入)”;

while优化课

如何用for来代替 while


number_of_kids =8

count = 0

while count
   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        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 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 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)的详细内容,更多请关注php中文网其它相关文章!

相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号