笔记25 笨办法习题35分支和函数路线图

这似乎是前面ex31游戏的一个延续,这么长的脚本录下来了,用ps执行,很快就纠正若干字码错误,顺利执行完毕。我先试着把这个代码的思路做个整理,理解各个指令的基本涵义再说。
为什么要引入exit,无意中找到查找exit的方法:

import sys
help(sys.exit)
Help on built-in function exit in module sys:
exit(status=None, /)
Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is an integer, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
这个指令如教材所言,当exit(0)时,程序中止,exit(1)时表示出错。在代码行14和代码行60中,以exit(0)使用表示停止。
整个代码,三个房间,使用5个函数定义。其中有四个定义函数使用了条件语句,如果把整个游戏代码整理为一张路线图,大概可以是这样一张图。借鉴了学友现成的一些工作,但做了一些改进,算是对于以前使用viso的一个复习,很长时间都没有使用整个软件,用起来还是有点费力,但还是弄成了。
练习ex35路线图的概括

对这个练习想做一些改动,但还是功力不够,改动的结果总不是原先设想的。弄清楚一个语言不是一个简单的事情,我把改动做的两个练习先展示,大概也是得再往前走多一些步子,你才能理解整个编码的内在联系的。
练习35.1.py

在这里插入代码片from sys import exit  # 引入exit函数待用,用于行60中def two_room():  #第一个函数,函数参数为空print("room1 is gold, room2 is bear, which one do you chose?")choice = input("> ")if "room1" in choice or "room2" in choice:which_room = [0, 1]if which_room == 0:print("Nice, you're right, you win!")exit(0)if which_room == 1:print("dangerious, you are in danger, be careful! ")else:dead("You will die, poorly!")def bear_room():print("There is a bear here.")print("The bear has a bunch of money.")print("The fat bear is in front of another door.")print("How are you going to move the bear?")bear_moved = Falsewhile True:choice = input("> ")if choice == "take honey":dead("The bear looks at you then slaps your face off.")elif choice == "taunt bear" and not bear_moved:print("The bear has moved from the door.")print("You can go through it now.")bear_moved = Trueelif choice == "taunt bear" and bear_moved:dead ("The bear gets pissed off and chews your leg off.")elif choice == "open door" and  bear_moved:gold_room()else:print("I got no idea what that means.")def cthulhu_room():print("Here you see the great evil Cthulhu.")print("He, it, whatever stares at you and you go insane.")print("Do you flee for your life or eat your head?")choice = input("> ")if "flee" in choice:start()elif "head" in choice:dead("Well that was tastly!")else:cthulhu_room()def dead(why):print(why, "Good job!")exit(0)def start():print("you are in a dark room. ")print("There is a door to your right and left.")print("Which one do you take?")choice = input("> ")if choice == "left":bear_room()elif choice == "right":cthulhu_room()else:dead("You stumble around the room until you starve.")start()``执行的结果完全不顾及在前的函数的修改,还理解不了,暂且搁置。现给出执行结果。
练习35.1.py的ps结果```python
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex35.1.py
you are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of money.
The fat bear is in front of another door.
How are you going to move the bear?
>
I got no idea what that means.
>
I got no idea what that means.
> taunt bear
The bear has moved from the door.
You can go through it now.
> open door
Traceback (most recent call last):File "ex35.1.py", line 76, in <module>start()File "ex35.1.py", line 69, in startbear_room()File "ex35.1.py", line 37, in bear_roomgold_room()
NameError: name 'gold_room' is not defined

再改一次,成ex35.2.py,依然不成功。
练习ex35.2.py

在这里插入代码片from sys import exit  # 引入exit函数待用,用于行60中def two_room():  #第一个函数,函数参数为空print("roomone is gold, room2 is bear, which one do you chose?")choice = input("> ")if "roomone" in choice or "roomtwo" in choice:which_room = [0, 1]if which_room == 0:print("Nice, you're right, you win!")exit(0)if which_room == 1:print("dangerious, you are in danger, be careful! ")else:dead("You will die, poorly!")def roomone():print("This room is full of gold. How much do you take?")choice = input("> ")if "0" in choice or "1" in choice:how_much = int(choice)else:dead("Man, type a number.")if how_much < 500:print("Nice, you win.")exit(0)def roomtwo():print("There is a bear here.")print("The bear has a bunch of money.")print("The fat bear is in front of another door.")print("How are you going to move the bear?")bear_moved = Falsewhile True:choice = input("> ")if choice == "take honey":dead("The bear looks at you then slaps your face off.")elif choice == "taunt bear" and not bear_moved:print("The bear has moved from the door.")print("You can go through it now.")bear_moved = Trueelif choice == "taunt bear" and bear_moved:dead ("The bear gets pissed off and chews your leg off.")elif choice == "open door" and  bear_moved:roomone()else:print("I got no idea what that means.")def roomthree():print("Here you see the great evil Cthulhu.")print("He, it, whatever stares at you and you go insane.")print("Do you flee for your life or eat your head?")choice = input("> ")if "flee" in choice:start()elif "head" in choice:dead("Well that was tastly!")else:roomthree()def dead(why):print(why, "Good job!")exit(0)def start():print("you are in a dark room. ")print("There is a door to your right and left.")print("Which one do you take?")choice = input("> ")if choice == "left":roomtwo()elif choice == "right":roomthree()else:dead("You stumble around the room until you starve.")start()

执行结果也让人意外,这个练习还是有些难度,存疑慢磨吧。
执行结果在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex35.2.py
you are in a dark room.
There is a door to your right and left.
Which one do you take?

left
There is a bear here.
The bear has a bunch of money.
The fat bear is in front of another door.
How are you going to move the bear?
taunt bear
The bear has moved from the door.
You can go through it now.
open door
This room is full of gold. How much do you take?
400
Nice, you win.
PS C:\Users\lenovo\1pythonw> python ex35.2.py
you are in a dark room.
There is a door to your right and left.
Which one do you take?
right
Here you see the great evil Cthulhu.
He, it, whatever stares at you and you go insane.
Do you flee for your life or eat your head?
flee
you are in a dark room.
There is a door to your right and left.
Which one do you take?
left
There is a bear here.
The bear has a bunch of money.
The fat bear is in front of another door.
How are you going to move the bear?
taunt bear
The bear has moved from the door.
You can go through it now.
take away
I got no idea what that means.
bear_moved
I got no idea what that means.
open
I got no idea what that means.
open door
This room is full of gold. How much do you take?
700
1
I got no idea what that means.
0
I got no idea what that means.
300
I got no idea what that means.
room
I got no idea what that means.
500
I got no idea what that means.
open door
This room is full of gold. How much do you take?
200
Nice, you win.
PS C:\Users\lenovo\1pythonw>

笔记25 笨办法习题35分支和函数路线图相关推荐

  1. 笔记14 笨办法中折磨人的习题ex23.py,人类语言与计算机语言

    笔记14 笨办法中折磨人的习题ex23.py,人类语言与计算机语言 开始学习习题23了,但为了那个languages.text的下载,费了老大劲好像还是没有下载全.后来载下来了,那个代码文件录下来,却 ...

  2. 笨办法学python练习35分支与函数

    1. 画一个这个游戏的流程图,并指出它是如何运转的. 3. 为你不理解的函数写上注释. 4. 为游戏增加一些功能,同时使代码更加简化. 这是原代码未经修改 from sys import exit d ...

  3. 《笨方法学 Python 3》35.分支和函数

    基础练习: from sys import exitdef gold_room():print("This room is full of gold. How much do you tak ...

  4. 【“笨办法”学Python】20.函数和文件

    20.函数和文件 文章目录 前言 一.Atom文本编辑器 二.运行Python程序 总结 前言   函数和文件在一起是如何一起协助发挥作用. 一.Atom文本编辑器 from sys import a ...

  5. Exercise 35: 分支和函数

    原文链接:http://learnpythonthehardway.org/book/ex35.html 你现在已经学习过了 if 语句,函数和列表.现在是时候考验下你了,输入下面这个实例,看看你是否 ...

  6. python 笔记 分支和函数《笨办法学Python》习题35 ——1.17

    习题 35:  分支和函数 知识点总结: • 本次做的是利用循环.判定做的一个小游戏 •fromsys import exit     #向sys模块借一个exit函数用来退出程序 •exit(0), ...

  7. “笨办法”学Python笔记 Lesson35—45

    一.作业内容 笨办法学 Python 习题35-45以及加分题. 二.作业代码: #习题35 分支和函数 from sys import exit def gold_room():print(&quo ...

  8. 笨办法学习python 学习笔记习题26

    笨办法学习python 学习笔记习题26 修改后如下: print("你多大了?", end=' ') age = input() print("你多高?", ...

  9. (读书笔记)笨办法学python-(习题1-40)

    文章目录 习题11:提问 问题15.16.17:文件的读写操作 问题20:函数和文件 习题21:函数可以返回东西 问题24:转译字符,格式化字符串 问题25:分词.单词排序.删除单词 问题31:通过i ...

最新文章

  1. ICLR20 | GraphZoom:可缩放图嵌入
  2. 表达不同与构建不同: 对计算机的唯一真正的要求 (TrustNo.1 ) -- 待修改!!!...
  3. linux 基础学习入门 2
  4. debian linux 版本代号
  5. 图片上传组件_博客必备功能,拖拽上传图片!
  6. 31.openssl编程——SSL实现
  7. Windows环境下MySQL 5.7的安装、配置与卸载
  8. chromebook刷机_您可以购买的最好的Chromebook,2017年版
  9. Hadoop中Partition解析
  10. python语言中的模块包括_python中常用的模块的总结
  11. java进阶学习 --java网络编程一(转)
  12. IOS发布APP Store
  13. HCIP2------BGP1
  14. 【冬季】寒冬已至,让这些公众号温暖你的冬天
  15. 护眼仪眼部按摩器单芯片蓝牙方案开发说明
  16. Internet Download Manager(V6.37版本IDM)免费序列号密钥激活版使用过程中的一些常见问题
  17. SCI常用词语及技巧 - 易智编译EaseEditing
  18. 天玥系列微型计算机,【简讯】AMD正式发布RX 6000系列显卡;OPPO K7x宣布…
  19. CentOS7L2TP/IPSec
  20. pychar调试报错:Cython extension speeds up Python debugging

热门文章

  1. 阿里云k8s一键部署有状态StatefulSet nacos2.0.3
  2. 龙格现象 图像对比及Python代码实现
  3. ubuntu系统20.4搭建c语言环境,ubuntu 20.04 中文环境和英文环境切换
  4. 光致发光量子效率(PLQY)计算软件-简易版本
  5. http缓存策略之强缓存与协商缓存
  6. STM32 热敏电阻NTC的软件设计(ADC采集)
  7. 泛微9.0明细表必填规则
  8. 年总结(六):半年历程总结(2016.6—2016.12)
  9. python数据分析分析(8G)学习视频免费分享
  10. 强! 看了这几个公众号让我拿到蚂蚁金服、美团的 Offer