下面是练习31-练习35,基于python3

 #ex31.py
1 print("You enter a dark room witn two doors. Do you go through door #1 or door #2?") door = input("> ") if door == "":
print("There's a giant bear here eating a cheese cake. What do you do?")
print("1. Take the cake.")
print("2. Scream at the bear.") bear = input("> ") if bear == "":
print("The bear eats your face off. Good job!")
elif bear == "":
print("The bear eats your legs off. Good job!")
else:
print("Well, doing %s is probably better. Bear runs away." %bear) elif door == "":
print("You stare into the endless abyss at Cthulhu's retina.")
print("1. Blueberries.")
print("2. Yellow jacket clothespins.")
print("3. Understanding revolvers yelling melodies.") insanity = input("> ") if insanity == "" or insanity == "":
print("Your body survives powered by a mind of jello. Good job!")
else:
print("The insanity rots your eyes into a pool of muck. Good job!") else:
print("You stumble around and fall on a knife and die. Good job!")
 #ex32.py
1 the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list
for number in the_count:
print("This is count %d" % number) # same as above
for fruit in fruits:
print("A ftuit of type: %s" % fruit) # also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
print("I got %r" % i) # we can also build lists, first start with an empty one
#elements = [] elements = range(0, 6)
# then use the range function to do 0 to 5 counts
#for i in range(0, 6):
# print("Adding %d to the list." % i)
# append is a function that lists understand
# elements.append(i) # now we can print them out too
for i in elements:
print("Element was: %d" % i)
 #ex33.py
1 i = 0
numbers = [] while i < 6:
print("At the top i is %d" % i)
numbers.append(i) i = i + 1
print("Numbers now: ", numbers)
print("At the bottom i is %d" % i) print("The numbers: ") for num in numbers:
print(num)
 #ex33+.py
1 # 加分习题
#函数里边的变量和脚本里边的变量之间是没有连接的
#所以定义函数必须返回numbers变量并重新给numbers赋值
def numbers_append(j,k):
i = 0
numbers = []
while i < j:
print("At the top i is %d" % i)
numbers.append(i) i = i + k
print("Numbers now: ", numbers)
print("At the bottom i is %d" % i)
return numbers numbers = numbers_append(6, 1) print("The numbers: ") for num in numbers:
print(num)
#ex34.py
1 animals = ['bear', 'tiger', 'penguin', 'zebra']
bear = animals[1]
print(bear)
 #ex35.py
1 #sys.exit() 引发一个 SystemExit异常,若没有捕获这个异常,Python解释器会直接退出;捕获这个异常可以做一些额外的清理工作。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。
from sys import exit def gold_room():
print("This room is full of gold. How much do you take?") next = input("> ")
if "" in next or "" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.") if how_much < 50:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!") def bear_room():
print("There is a bear here.")
print("The bear has a bunch of honey.")
print("The fat bear is in front of another door.")
print("How are you going to move the bear?")
bear_moved = False
while True:
next = input("> ") if next == "take honey":
dead("The bear looks at you then slaps your face off.") elif next == "taunt bear" and not bear_moved:
print("The bear has moved from the door. You can go through it now.")
bear_moved = True elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed off and chews your leg off.") elif next == "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?") next = input("> ") if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
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?") next = input("> ") if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.") start()

笨办法学Python(learn python the hard way)--练习程序31-35的更多相关文章

  1. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  2. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  3. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  4. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

  5. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  6. 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘

    笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln  怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...

  7. 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍

    点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...

  8. 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程

    <笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...

  9. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

  10. 《笨办法学Python 3》python入门书籍推荐|附下载方式

    <笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...

随机推荐

  1. VMWARE 克隆步骤

    克隆linux服务器点击设置 ->网络适配器->高级->MAC地址 重新生成一个 OK

  2. day17跨文件夹导入模块,模块的两种被执行方式,包,直接使用包中模块,包的管理

    复习 ''' 1.模块 -- 一系列功能的集合体,用文件来管理一系列有联系的功能,该文件我们称之为模块,文件名就是模块名 -- import | from...import 来导入模块,从而使用模块中 ...

  3. Hadoop运行模式:本地模式、伪分布模式、完全分布模式

    1.本地模式:默认模式 - 不对配置文件进行修改. - 使用本地文件系统,而不是分布式文件系统. - Hadoop不会启动NameNode.DataNode.ResourceManager.NodeM ...

  4. vue2.0 watch里面的 deep和immediate作用

    deep,默认值是 false,代表是否深度监听.immediate:true代表如果在 wacth 里声明了之后,就会立即先去执行里面的handler方法,如果为 false就跟我们以前的效果一样, ...

  5. STS安装Drools

    download drools (including "Drools Engine" & "Drools and jBPM tools") from o ...

  6. Spring-Boot 整合Dubbo 解决@Reference 注解为null情况

    首先检查一下你的spring boot版本是多少? 如果是2.X 不用看了,spring boot 2.x 必定会出现这个问题, 改为 1.5.9 或其他1.x版本,目前生产环境建议使用1.x版本. ...

  7. 三连击 P1008 洛谷 python写法

    三连击 P1008 洛谷 题意 将\(1,2, \cdots,9\)共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 解题思路 这里我使用了 ...

  8. 打开虚拟机提示 无法获得vmci 驱动程序的版本:句柄无效

    我从另一台电脑复制过来虚拟机,提示如题. 找到  我的虚拟机的  *.vmx文件(如NeoKylin.vmx),其中有 vmci0.present = "TRUE",将TRUE改为 ...

  9. Python的__pycache__的文件夹

    · 前言 用python编写好一个工程,在第一次运行后,总会发现工程根目录下生成了一个__pycache__文件夹,里面是和py文件同名的各种 *.pyc 或者 *.pyo 文件. 先大概了解一下py ...

  10. openstack stein部署手册 10. horzion

    # 安装程序包 yum install -y openstack-dashboard # 变更配置文件 /etc/openstack-dashboard/local_settings 变更以下 OPE ...