下面是练习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. 学习《Oracle PL/SQL 实例讲解 原书第5版》----创建账户

    通过readme.pdf创建student账户. 以下用sys账户登录时都是sysdba. 一.PL/SQL 登录oracle. SYS/123  AS SYSDBA 账户名:sys:密码:123:作 ...

  2. django-xadmin设置全局变量

    class GlobalSetting(object): site_title = '自己的命名' site_footer = '底部命名'# 收缩菜单 menu_style = 'accordion ...

  3. python基础-5.1几种常见的排序算法

    一.冒泡排序(BubbleSort) 步骤: 比较相邻的元素,如果第一个比第二个大,就交换他们两个. 循环一遍后,最大的数就“浮”到了列表最后的位置. 将剩下的数再次循环,直道所有的排序完成 def ...

  4. Nacos1.1.3小试牛刀

    什么是 Nacos(摘自https://nacos.io/zh-cn/docs/quick-start.html) Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性 ...

  5. 1 Python 新建项目

    1 新建项目->新建Python文件 2导入package 库文件 3 import 类似using #include 4 写完代码编译 默认debug的对象是第一个创建的py文件,后续写的文件 ...

  6. 《剑指offer》面试题17 合并两个排序的链表 Java版

    我的方法:新初始化一个链表头,比较两个链表当前节点的大小,然后连接到该链表中.遍历两个链表直到null为止. public ListNode merge(ListNode first, ListNod ...

  7. HDU 5094 题解(状压BFS)

    题面: Maze 题目中文大意: 这个故事发生在“星际迷航”的背景下. “星际争霸”的副队长史波克落入克林贡的诡计中,被关押在他们的母亲星球Qo’noS上. 企业的上尉詹姆斯·T·柯克(James T ...

  8. Python自学第二天学习之《元组与字典》

    一.  元组:tuple类型,元组一级元素 不能修改 不能增加 不能删除,是有序的. 格式 :tu=(1,2,3,4,5,6) 1.类型转换: #字符串转换成元组 b=“123” c=tuple(b) ...

  9. Vue+axios+Node+express实现文件上传(用户头像上传)

    Vue 页面的代码 <label for='my_file' class="theme-color"> <mu-icon left value="bac ...

  10. 防止 iframe 的链接重定向父级页面

    项目中发现,多系统通过iframe嵌套时,如果iframe的请求是重定向会导致父级页面重定向,怎么破? 查找MDN(https://developer.mozilla.org/zh-CN/docs), ...