Exercise 35: Branches and Functions

from sys import exit
def gold_room():
    print "This room is full of gold. How much do you take?"
    next = raw_input("> ")
    if "0" in next or "1" 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 = raw_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 = raw_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 = raw_input("> ")
  if next == "left":
    bear_room()
  elif next == "right":
    cthulhu_room()
  else:
    dead("You stumble around the room until you starve.")
start()                

What does exit(0) do?
On many operating systems a program can abort with exit(0), and the number passed in will indicate an error or
not. If you do exit(1) then it will be an error, but exit(0) will be a good exit. The reason it‘s backward from normal
boolean logic (with 0==False is that you can use different numbers to indicate different error results. Such as you can
do exit(100) for a different error result than exit(2) or exit(1).

时间: 2024-10-03 15:47:47

Exercise 35: Branches and Functions的相关文章

Python Index

Table Of ContentsThe Hard Way Is EasierExercise 0: The SetupExercise 1: A Good First ProgramExercise 2: Comments And Pound CharactersExercise 3: Numbers And MathExercise 4: Variables And NamesExercise 5: More Variables And PrintingExercise 6: Strings

笨方法学Python,Lesson 35, 36

Exercise 35 代码 from sys import exit  def gold_room():     print "This room is full of gold. How much do you take?"          choice = raw_input("> ")     if "0" in choice or "1" in choice:         how_much = int(c

行为型设计模式之迭代器模式

结构 意图 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示. 适用性 访问一个聚合对象的内容而无需暴露它的内部表示. 支持对聚合对象的多种遍历. 为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代). 1 using System; 2 using System.Collections; 3 4 class Node 5 { 6 private string name; 7 public string Name 8 { 9 get 10 { 11 return

dfsdf

This project was bootstrapped with Create React App. Below you will find some information on how to perform common tasks. You can find the most recent version of this guide here. Updating to New Releases Create React App is divided into two packages:

Concurrent Programming(4)

Using Threads for Parallelism Figure 12.30 shows the set relationships between sequential, concurrent, and parallel programs. A parallel program is a concurrent program running on multiple processors. Running time actually increases a bit as we incre

超炫的粒子效果!代码,就应该这么写!

最近瞎逛的时候发现了一个超炫的粒子进度效果,有多炫呢?请擦亮眼镜! 粗略一看真的被惊艳到了,而且很实用啊有木有!这是 Jack Rugile 写的一个小效果,源码当然是有的.聪慧如你,肯定觉得这个东西so easy 要看啥源码,给我3分钟我就写出来了吧.所以你的思路可能是: 1)进度条的实现没什么好说的,简单的一个 fillRect(0,0,long,20),long和20是分别进度条的长宽.然后每帧动画调用前将画布清除clearRect(0,0,canvas.width,canvas.heig

board_key.h/board_key.c

1 /******************************************************************************* 2 Filename: board_key.h 3 Revised: $Date: 2014-02-28 14:18:14 -0800 (Fri, 28 Feb 2014) $ 4 Revision: $Revision: 37461 $ 5 6 Description: This file contains the SRF06EB

[Webpack 2] Ensure all source files are included in test coverage reports with Webpack

If you’re only instrumenting the files in your project that are under test then your code coverage report will be misleading and it will be difficult for you to track or enforce improvements to application coverage over time. In this lesson we’ll lea

[Webpack 2] Intro to the Production Webpack Course

There are several lessons that will build on top of this project. It is a fairly standard, small webpack bundled project. In this lesson we’ll explore the project a bit so you’re familiar with how things are set up for future lessons. In Webpack 2, t