20170928习题

ex36

#分支和函数from sys import exit        #导入system模块中的exit函数

def gold_room():            #定义金子房间的函数    print("this room is full of gold,how much do you take?")    next = input(">")       #输入内容

# if "0" in next or "1" in next:    #     how_much = int(next)

if int(next) >= 0:      #改进后判断输入是否为数值        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("this 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 money":            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 chew your leg off.")        elif next =="open the door" and bear_moved:            gold_room()        else:            print("i got no idea what the means.")def cthulhu_room():         #定义恶魔房间的函数    print("here you see the great evil leftcthulhu.")    print("he,it,whatever stares at you and you go insane.")    print("do you flee for your life or eat your life.")

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()-----------------------------------------------------------------------ex.39
#列表的操作

ten_things = "apples oranges crows telephone light sugar"print("wait,there is not 10 things in that list,let‘s fix it.")

stuff = ten_things.split(" ")       #split为切割列表指定内容more_stuff = ["day","night","song","frisbee","corn","banana","girl","boy"]

while len(stuff) != 10:             #len为计算列表内容的长度    next_one = more_stuff.pop()     #pop为取得列表内某一内容,不指定参数时,默认取得最后一个    print("adding",next_one)    stuff.append(next_one)          #append为列表内增加指定内容    print("there is %d items now" %len(stuff))

print("there we go:",stuff)print("let‘s do something with stuff.")

print(stuff[1])                     #stuff[1]为列表内索引值为1的内容print(stuff[-1])                    #stuff[-1]为列表内索引值为-1的内容,就是从尾部数第一的print(stuff.pop())print(‘ ‘.join(stuff))print(‘#‘.join(stuff[3:5]))         #join向列表内指定位置增加指定内容------------------------------------------------------------------------------------------ex40
#字典、可爱的字典

cities = {‘ca‘:‘san francisco‘,‘mi‘:‘detroit‘,‘fl‘:‘jacksonville‘}cities[‘ny‘] = ‘new york‘cities[‘or‘] = ‘portland‘

def find_city(themap,state):                #定义一个函数,参数是themap和state    if state in themap:                     #判断,如果state在themap里,返回一个themap值        return themap[state]    else :                                  #否则,返回没找到        return "not found"#ok,pay attention!cities[‘_find‘] = find_city                 #向字典内增加一个键值对,键是_find,值是[find_city]函数while True:    print("state?(enter to quit)")    state = input(‘>‘)    if not state :break    #this line is the most important ever!study!    city_found = cities[‘_find‘](cities,state)  #调用函数,find_city(cities,state),返回states对应的city,赋值给变量    print(city_found)---------------------------------------------------------------------------------------------------------------------2017-09-28    22:38:30
时间: 2024-10-16 09:07:30

20170928习题的相关文章

数据库经典习题,

/* 数据导入: Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : sqlexam Target Server Type : MySQL Target Server Version : 50624 File Encoding : utf-8

C/C++算法竞赛入门经典Page15 习题1-1 平均数

题目:输入3个整数,输出他们的平均值,保留3位小数. 首先,声明三个整数a,b,c和一个浮点数d: int a,b,c; double d; 输入三个整数a,b,c: scanf("%d%d%d",&a,&b,&c); 将a,b,c取平均值以后复制给d: d=(double)(a+b+c)/3; 最后输出d: printf("%.3lf",d); %.3lf表示保留3位小数的long float. 注意:不能直接这样输出: printf(&q

问题 1018: C语言程序设计教程(第三版)课后习题6.8

/******************************************************************** @file Main.cpp @date 2017-05-12 @author Zoro_Tiger @brief 问题 1018: C语言程序设计教程(第三版)课后习题6.8 http://www.dotcpp.com/oj/problem1018.html *************************************************

SICP 习题 (1.46)解题总结

SICP 习题 1.46 要求我们写一个过程iterative-improve,它以两个过程为参数,其中一个参数用来检测猜测是否足够好,另一个参数用来改进猜测.过程iterative-improve应该返回另一个过程,所返回的过程接收一个参数作为初始猜测,然后不断改进猜测直到结果足够好.题目还要求我们使用iterative-improve重写1.1.7的sqrt过程和1.3.3节的fixed-point过程. 因为涉及到高阶函数,所以整个题目理解起来有一点点费劲.不过这道题作为第一章的收官题确实

C++ Primer 学习笔记_74_面向对象编程 --再谈文本查询示例[续/习题]

面向对象编程 --再谈文本查询示例[续/习题] //P522 习题15.41 //1 in TextQuery.h #ifndef TEXTQUERY_H_INCLUDED #define TEXTQUERY_H_INCLUDED #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <set> #include <map&g

pta 数据结构 习题2.4 递增的整数序列链表的插入(15 分)

习题2.4 递增的整数序列链表的插入(15 分) 本题要求实现一个函数,在递增的整数序列链表(带头结点)中插入一个新整数,并保持该序列的有序性. 函数接口定义: List Insert( List L, ElementType X ); 其中List结构定义如下: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; type

linux习题回顾

linux习题回顾 1.1 创建一个压缩包/etc,我想让压缩包上面有个日期/时间. [[email protected] ~]# tar zcf /tmp/etc-$(date+%F).tar.gz /etc [[email protected] ~]# ls -l /tmp -rw-r--r--. 1 root root 9731838 Aug  3 19:15 etc-2017-08-03.tar.gz 1.2 已知/oldboy/test.txt文件内容为: oldboy xizi xi

问题 1041: C语言程序设计教程(第三版)课后习题9.8

/******************************************************************** @file Main.cpp @date 2017-05-28 22:02:55 @author Zoro_Tiger @brief 问题 1041: C语言程序设计教程(第三版)课后习题9.8 http://www.dotcpp.com/oj/problem1041.html ****************************************

问题 1040: C语言程序设计教程(第三版)课后习题9.6

/******************************************************************** @file Main.cpp @date 2017-05-28 21:57:02 @author Zoro_Tiger @brief 问题 1040: C语言程序设计教程(第三版)课后习题9.6 http://www.dotcpp.com/oj/problem1040.html ****************************************