下面是练习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. How are you to imagine anything if the images are always provided for you?

    perdestrian: n. 行人 compliment: n. 赞扬 simply: adv. 只是,仅仅 shorten: vt. 缩短 accustom: vt. 习惯 collide: v. ...

  2. "SQLServer复制需要有实际的服务器名称才能连接到服务器,请指定实际的服务器名"

    最近在学习SQL SERVER的高级复制技术的时候,遇到一个小问题,就是用本地SQL SERVER连接服务器的数据库时,在查看复制功能的发布服务器时,连接不上,弹出一个错误提示框架,如下:原来在自己本 ...

  3. Spring Boot系列(四) Spring Boot 之验证

    这节没有高深的东西, 但有一些学习思路值得借鉴. JSR 303 (Bean Validation) Maven依赖 <dependency> <groupId>org.spr ...

  4. 第四周实验报告&实验总结

    试验报告2 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...

  5. winCE 获取路径信息

    最近在做一个SAP的winCE扫描枪项目,采用C#开发,不过在获取路径是采用了常用的System.IO.Directory.GetCurrentDirectory, 并不能使用:查询后了解到winCE ...

  6. github廖雪峰git

    gitHub地址: https://github.com/DickyQie/Tool-use/tree/git-learning-document

  7. vue观察模式浅析

    以下是我对vue观察者模式的理解:github L6zt 加入tip 2018-10-14 最近又看到<js设计模式设计>书 推荐去搂搂不要对框架的偏见, 你真的了解jquery.angu ...

  8. python Calendar 模块导入及用法

    Calendar 是python 日历模块,此模块的函数都是日历相关的,例如打印某月的字符月历,星期之类的模块,下面剖析python Calendar 模块导入及用法. 1,python导入日历模块 ...

  9. Leetcode Lect3 内存中的栈空间与堆空间

    内存中的栈空间与堆空间 我们通常所说的内存空间,包含了两个部分:栈空间(Stack space)和堆空间(Heap space) 当一个程序在执行的时候,操作系统为了让进程可以使用一些固定的不被其他进 ...

  10. 使用JS提交表单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...