《笨办法学python》习题 35: 分支和函数

from sys import exit#导入模块

def gold_room():#定义gold_room函数
    print('This room is full of gold.How much do you take?')
    next=input('>')#用户输出next内容
    if '0' in next or '1' in next:#如果输入内容包含0或者1
        how_much=int(next)#how_much值为next的整形
        if how_much < 50:# 如果how_much小于50
            print('Nice,you\'re not greedy,you win!')  # 打印you win
            exit(0)  # 结束游戏
        else:  # 否则打印Your greedy bastard
            dead('Your greedy bastard!')
    else:#打印***,Good job
        dead('Man,learn to type a number.')

def bear_room():#定义bear_room函数
    print('There is a bear here.')
    print('The bear has a bunch of money.')
    print('The fat bear is in front of another door.'
          '\nHow are you going to move the bear?')
    bear_moved=False#bear_moved初始值为False
    while True:#执行下面函数
        next=input('>')#用户输入next值
        if next=='take honey':#如果输入的值是take honey,打印***,good job
            dead('The bear looks at you then slaps your face off.')
        elif next=='taunt bear' and not bear_moved:#如果输入的值是taunt bear且bear_moved是False,打印***
            print('the bear has moved from the door.You can go through it now ')
            bear_moved=True
        elif next=='taunt bear' and bear_moved:#如果输入的值是taunt bear且bear_moved是True,打印***
            dead('The bear gets pissed off and chews your leg off.')
        elif next=='open door' and bear_moved:#如果输入的值是pen doorr且bear_moved是True,调用gold_room()
            gold_room()#调用gold_room函数
        else:#否则打印I got no idea what that means
            print('I got no idea what that means.')

def cthulhu_room():#定义cthulhu_room函数
    print('Here you see the great evil Chtulhu.'
          '\nHe,it,whaterer stares at you and you go insane'
          '\nDo you flee for your life or eat your head.')
    next=input('>')#用户输入next内容
    if 'flee' in next:#如果用户输入想跑
        start()#开始
    elif 'head' in next:#如果用户输入吃到脑袋
        dead('Well that was tasty!')#打印值,结束
    else:#否则继续调用cthulhu_room()函数
        cthulhu_room()

def dead(why):#定义dead函数,打印**,good job
    print(why,"Good job!")
    exit(0)#中断程序运行

def start():#定义函数start()
    print('You are in a dark room.'
          '\nThere is a door to your right and left.'
          '\nWhich one do you take?')
    next=input('>')#提示用户输入内容
    if next=='left':#如果用户输入左边,调用bear_room函数
        bear_room()
    elif next=='right':#如果用户输入右边,调用cthulhu_room函数
        cthulhu_room()
    else:#否则,打印***,Good job
        dead('You sumble around the room until you starve.')



start()#调用start()函数,程序开始运行

调试:

1)elif next=='taunt bear' and not bear_moved比较懵逼,后来 百度了一下指的是bear_moved=False

2)运行的时候发现跟书中结果不一样,最后发现是dead(why)函数中print内容why加了单引号,而且写成了Why

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值