ex34~ex35一個游戲

ex35寫了一個游戲,代碼貼上

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(choice)
    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 g bunch of honey."
    print "The fat bear is in front of another door."
    print "How are your going to move the bear?"
    bear_move = False

    while True:
        choice = raw_input("> ")

        if choice == "take honey":
            dead("The bear looks at your then slaps your face off.")
        elif choice == "taunt bear" and not bear_move:
            print "The bear has moved from the door. You can go through in now."
            bear_move = True
        elif choice == "taunt bear" and bear_move:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "open door" and bear_move:
            gold_room()
        else:
            print "I got no idea what thet 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?"

    choice = raw_input("> ")

    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that waw 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?"

    choice = raw_input("> ")

    if choice == "left":
        bear_room()
    elif choice == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")

start()

上面有一句代碼有Bug,就是這一句 if "0" in choice or "1" in choice:因?yàn)橥ㄟ^raw_input()接收到的都是字符串類型,并不能直接判斷是否為數(shù)字,我上網(wǎng)搜了一下,找到一個方法:

s.isdigit() 所有字符都是數(shù)字,為真返回 Ture,否則返回 False。

再結(jié)合上面的代碼,這個問題就解決了,原代碼寫的很奇怪

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • http://python.jobbole.com/85231/ 關(guān)于專業(yè)技能寫完項(xiàng)目接著寫寫一名3年工作經(jīng)驗(yàn)的J...
    燕京博士閱讀 7,828評論 1 118
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • Python簡介 Python歷史 Python 是由 Guido van Rossum 在八十年代末和九十年代初...
    莫名其妙的一生閱讀 1,144評論 0 2
  • 今天雖然講完了ppt,蛋用沒有放松的感覺,覺得老師現(xiàn)在有點(diǎn)不喜歡我,我覺得我做的每一件事他都不喜歡,好郁悶啊,,,...
    姬旦雞蛋一樣閱讀 234評論 0 0
  • 我從山中走來 像土豆 低價卻不低廉 食多脹氣 但更能維持生命 我從山中走來 像小草 孤獨(dú)的在風(fēng)中搖曳 倔強(qiáng)而又不屈...
    南山臺子閱讀 466評論 0 2

友情鏈接更多精彩內(nèi)容