Skip to content

Latest commit

 

History

History
396 lines (325 loc) · 8.61 KB

File metadata and controls

396 lines (325 loc) · 8.61 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0175
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在: 

     个人网站: `https://oeasy.org` 
     蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
     GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
     Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

break 中断条件

回忆

  • 这次了解了 break
    • break 可以打破循环

图片描述

  • 能否输出
    • 洪信范围内 所有的中文字符呢?
    • 怎么办?🤔

数字次序

  • 洪 在 信 后面
    • 我们只需要
    • 倒着找字符就可以
c = '洪'
while c >= '信':
    if ord(c) % 16 == 0:
        print("\n" + str(hex(ord(c))) + "\t",end = "")
    print(c, end=" ")
    c = chr(ord(c) - 1)

  • 效果

图片描述

原因

  • 原来是次序问题

图片描述

  • 我还是想要
    • 遇洪而开
    • 见信而寂
  • 每次要字符减一就可以到达了吧?!

动手

  • 这样就可以得到最终结果的!!!
    • 遇洪而开
    • 见信而寂

图片描述

  • 正着遍历不行
    • 换种思路倒着着遍历
    • 完成了任务

逆向思维

  • 这条偈(jì)语
    • 来自于五代时期宁波布袋和尚

图片描述

  • 布袋和尚总拿着一个布袋

    • 我有一布袋
    • 虚空无挂碍
    • 打开遍十方
    • 入时观自在。
  • 不要陷入任何固定的思维模式

    • 时刻维持 高学习率
  • break 什么意思来着?

break

图片描述

  • 游乐场的帮助手册告诉我们
    • break 跳出当前的循环
    • 跳出三界外
    • 不在五行中
  • 所以我们期待的 是
    • 剥极而复
    • 否极泰来
    • 周而复始

zigzag

  • 循环往复
    • 春夏秋冬
    • 东南西北
    • 苦尽甘来
import	sys
import	time
indent	=	0
indentIncreasing=True
try:
	while	True:
		print(' '*indent,	end='')
		print('********')
		time.sleep(0.1)
		if	indentIncreasing:
			indent =	indent+1
			if	indent ==	20:
				indentIncreasing =	False
		else:
			indent =	indent-1
			if indent ==	0:
				indentIncreasing = True
except	KeyboardInterrupt:
	sys.exit()
  • 跳出循环 用的是
    • try ... except ...
    • 的方式

图片描述

  • 无限的循环当中
    • 总有 特殊的情况发生

悟空

  • 跳出条件
    • 跳出三界外 不在五行中

图片描述

  • 鸿蒙初辟原无姓,打破顽空须悟空
  • 可以用ai辅助我们控制流程吗?

回忆赛博炼剑

图片描述

  • 最后一轮 我被击倒
    • 死后发出的最后一击
    • 如何修改
# @Version : 2.0  
# @Time    : 2022/09/25  
# @Author  : oeasy

import time

print('=========== Welcome to Underground! ============')
print('''
     ,            _..._            ,
    {'.         .'     '.         .'}
    { ~ '.      _|=    __|_      .'  ~}
  { ~  ~ '-._ (___________) _.-'~  ~  }
 {~  ~  ~   ~.'           '. ~    ~    }
{  ~   ~  ~ /   /\     /\   \   ~    ~  }
{   ~   ~  /    __     __    \ ~   ~    }
 {   ~  /\/  -<( o)   ( o)>-  \/\ ~   ~}
  { ~   ;(      \/ .-. \/      );   ~ }
   { ~ ~\_  ()  ^ (   ) ^  ()  _/ ~  }
    '-._~ \   (`-._'-'_.-')   / ~_.-'
        '--\   `'._'+'_.'`   /--'
            \     \`-'/     /
             `\    '-'    /'
               `\       /'
                 '-...-' 
''')

hp = 0
att = 0
defend = 0
monsterLevel = 0

job = input('''Select Your Job(a or b):
            A:Warrior
            B:Magic
            '''
           )
while (job != "a" and job != "A" and job != "b" and job != "B"):
    print("Input Error,Please input a or b")
    job = input('''Select Your Job:
            A:Warrior
            B:Magic
            ''')

level = int(input('Select Your Level:') )
monsterLevel = int(input('Select Enemy Level:') )             
             
if job == "a" or job == "A":
    hp = 800 + level * 59
    att = 100 + level * 10
    defend = 20 + level * 5


if job == "b" or job == "B":
    hp = 500 + level * 35
    att = 120 + level * 19
    defend = 15 + level * 3
        
print('You Level is 是{},Attack{},Hp{},Defence{}' .format(level, att, hp,defend))
print("=========== Fight began ==============")
bosshp = 10000 + monsterLevel*30
bossatt = 50 + monsterLevel * 8
bossdef = 50

while bosshp >= 0 and hp >= 0:
    hp = hp - bossatt
    print("\33[41mBoss\33[0m attack \33[42myou\33[0m!,HP - {},HP remain:{}".format(bossatt, hp))
    bosshp = bosshp - att;
    print("\33[42mYou\33[0m attack \33[41mBoss\33[0m,Hit {} ,remain HP:{}".format(att,bosshp))
    print("==================================")
    
    time.sleep(0.5)
    
    if hp <= 0:
        print("You lose ,Try again! (Maybe you should improve your level")
    if bosshp <= 0:
        print("You Win!And Got a sword!")
        print('''
           ,
          / \\
         {   }
         !   !
         ; : ;
         | : |
         | : |
         l ; l
         l ; l
         I ; I
         I ; I
         I ; I
         I ; I
         d | b 
         H | H
         H | H
         H I H
 ,;,     H I H     ,;,
;H@H;    ;_H_;,   ;H@H;
`\Y/d_,;|4H@HK|;,_b\Y/'
 '\;MMMMM$@@@$MMMMM;/'
   ~~~*; !8@8!; *~~~
         ;888;
         ;888;
         ;888;
         ;888;
         d8@8b
         O8@8O
         T808T
          `~` 
''')

ai分析结果

# @Version : 2.0  
# @Time    : 2022/09/25  
# @Author  : oeasy

import time

print('=========== Welcome to Underground! ============')
print('''
     ,            _..._            ,
    {'.         .'     '.         .'}
    { ~ '.      _|=    __|_      .'  ~}
  { ~  ~ '-._ (___________) _.-'~  ~  }
 {~  ~  ~   ~.'           '. ~    ~    }
{  ~   ~  ~ /   /\     /\   \   ~    ~  }
{   ~   ~  /    __     __    \ ~   ~    }
 {   ~  /\/  -<( o)   ( o)>-  \/\ ~   ~}
  { ~   ;(      \/ .-. \/      );   ~ }
   { ~ ~\_  ()  ^ (   ) ^  ()  _/ ~  }
    '-._~ \   (`-._'-'_.-')   / ~_.-'
        '--\   `'._'+'_.'`   /--'
            \     \`-'/     /
             `\    '-'    /'
               `\       /'
                 '-...-' 
''')

hp = 0
att = 0
defend = 0
monsterLevel = 0

job = input('''Select Your Job(a or b):
            A:Warrior
            B:Magic
            '''
           )
while (job != "a" and job != "A" and job != "b" and job != "B"):
    print("Input Error,Please input a or b")
    job = input('''Select Your Job:
            A:Warrior
            B:Magic
            ''')

level = int(input('Select Your Level:') )
monsterLevel = int(input('Select Enemy Level:') )             
             
if job == "a" or job == "A":
    hp = 800 + level * 59
    att = 100 + level * 10
    defend = 20 + level * 5


if job == "b" or job == "B":
    hp = 500 + level * 35
    att = 120 + level * 19
    defend = 15 + level * 3
        
print('You Level is {},Attack {},Hp {},Defence {}'.format(level, att, hp, defend))
print("=========== Fight began ==============")
bosshp = 10000 + monsterLevel*30
bossatt = 50 + monsterLevel * 8
bossdef = 50

while bosshp >= 0 and hp >= 0:
    hp = hp - bossatt
    print("\33[41mBoss\33[0m attack \33[42myou\33[0m!,HP - {},HP remain:{}".format(bossatt, hp))
    
    # 检查玩家血量是否为负数,如果是则跳出循环
    if hp <= 0:
        print("You lose ,Try again! (Maybe you should improve your level)")
        break
    
    bosshp = bosshp - att;
    print("\33[42mYou\33[0m attack \33[41mBoss\33[0m,Hit {} ,remain HP:{}".format(att, bosshp))
    print("==================================")
    
    time.sleep(0.5)
    
    if bosshp <= 0:
        print("You Win!And Got a sword!")
        print('''
           ,
          / \\
         {   }
         !   !
         ; : ;
         | : |
         | : |
         l ; l
         l ; l
         I ; I
         I ; I
         I ; I
         I ; I
         d | b 
         H | H
         H | H
         H I H
 ,;,     H I H     ,;,
;H@H;    ;_H_;,   ;H@H;
`\Y/d_,;|4H@HK|;,_b\Y/'
 '\;MMMMM$@@@$MMMMM;/'
   ~~~*; !8@8!; *~~~
         ;888;
         ;888;
         ;888;
         ;888;
         d8@8b
         O8@8O
         T808T
          `~` 
''')

总结

  • 这次了解了 break
  • break 可以打破循环
    • 跳出三界外
    • 不在五行中
  • 现在的问题是
    • 遇洪而开
    • 见信而寂
  • 无法输出任何字符
  • 怎么办?🤔
  • 下次再说 👋

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。