2016-07-30 4 views
-3

내가 파이썬 2.7를 사용하여 파이썬 하드 방법의 책을 배우고 :-(. . 내가 몇 가지 창의적인 아이디어를 가지고, 그러나

나를 혼란 나는이 연습의 일부 변경 만들고 싶어 : http://learnpythonthehardway.org/book/ex35.html

을 그래서

from sys import exit 

times_in_exchange_path = 0 
asked_for_help = 0 

def exchange_path(): 
    print "Hola! You are about to get to the very dangerous point, kid!" 
    print "You are independent from now. With luck..." 

    action = raw_input('...') 

    if 'what' in action: 
     print 'I said you are on your own now. No one is gonna help you', 
     print "with anything..." 
     exchange_path() 
    elif 'creepy' in action: 
     print "I found it creepy, too. Kid. Take me out of here with you." 
     print "Is that OK?" 
     haunted = False 

     decision = raw_input() 

     if decision == 'agree': 
      print "Finally. I'm free!" 
      haunted = True 
      times_in_exchange_path = times_in_exchange_path + 1 
      start() 
     else: 
      dead("I will make you know what pain really is!!?! >:(") 

    else: 
     dead("A shadow barged into you, and you broke your neck.") 

def gold_room(): 
    print "This room is full of gold. How much do you take?" 

    choice = raw_input("> ") 
    try: 
     how_much = int(choice) 
    except: 
     dead("Man, learn to type a number.") 

    if how_much < 50: 
     print "Nice, you're not greedy, you win!" 

     if haunted: 
      print "\nHowever... I can't just let you get away easily like this." 
      print '"Why??" - I asked' 
      print "You belong to the gold mine now!!" 
      print '"What do you mean??" - I replied in fear' 
      print "Power requires sacrifice. Muhahahhaha!" 
      dead(" ") 
     else: 
      exit(0) 

    else: 
     dead("You greedy bastard! It's a trap for you!") 

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?\n" 
    bear_moved = False 

    if haunted: 
     print "...Wait. The bear suddenly shouts at you, break the window", 
     print "and runs far far away." 
     print "Let's go to the door, kid...\n\n" 
     gold_room() 
    else: 
     while True: 
      choice = raw_input("> ") 

      if choice == "take honey": 
       dead("The bear looks at you then slaps your face off.") 
      elif choice == "taunt bear" and not bear_moved: 
       print "The bear has moved from the door. You can go through", 
       print "it now." 
       bear_moved = True 
      elif choice == "taunt bear" and bear_moved: 
       dead("The bear gets pissed off and chews your leg off.") 
      elif choice == "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 goes insane." 
    print "Do you flee for your life or he eats your head?" 
    cthulhu_dead = False 

    choice = raw_input("> ") 

    if cthulhu_dead: 
     print "There's nothing here, kid. Except of the light and a dead body." 
     print "Let's go!\n...\n\n" 
     start() 
    else: 

     if "flee" in choice: 
      start() 
     elif "head" in choice: 
      dead("Well that was tasty!") 
     elif "fight" in choice: 

      if haunted: 
       print "With mysterious distraction, the Cthulhu turns his back", 
       print "at you. You use the shiny magical spear nearby to kill", 
       print "the monster." 
       cthulhu_dead = True 
       print "\n...\n" 
       start() 
      else: 
       dead("Awesome tasty head you have!") 

     else: 
      cthulhu_room() 


def dead(why): 
    print why, "\n\nGood job! You died." 
    exit(0) 


def first_assist(): 
    asked_for_help += 1 

    if asked_for_help in range(4, 6): 
     print "Stop! You are attracting them!" 
     print "A portal shows up and you are walking into it...\n\n" 
    elif asked_for_help > 5: 
     dead("Those demons rise from the ground. And burn you down!") 
    else: 
     print "Hi! I am your beautiful assistance." 
     print "As you got lost in here, I will show you the way." 
     print "You have to know that all three paths are dangerous, and there", 
     print "is no way out of here.\nSo, try to survive with glory." 
     print '"left", "right" and "front" are the first three spells you use.' 
     print "There's a mighty power near here. Have a pure heart, and know", 
     print "that you will never have to touch that evil power..." 
     print "Good luck from here... Be wise." 
    start() 


def start(): 
    print "You are in a dark room." 
    print "There is a door to your right and left." 
    print 'And a middle path, full of darkness, in front of you.' 
    print "Which one do you dare to take?" 

    choice = raw_input("> ") 

    if choice == "left": 
     bear_room() 
    elif choice == "right": 
     cthulhu_room() 
    elif choice == "front": 

     if times_in_exchange_path == 0: 
      exchange_path() 
     else: 
      print "You feel something stopping you from entering this place. \n" 
      start() 

    elif "help" in choice: 
     first_assist() 
    else: 
     dead("You stumble around the room until you starve.") 


start() 

그러나 어떤 이유로, 내 코드가 제대로 작동하지 않습니다 그것은 첫째 인쇄

을 줄이 같은 화면에서 : 내 자신의 코드를 썼습니다.

어두운 방에 있습니다.

이 오른쪽으로 문이며, 당신이 취할 감히 어느 어둠

의 전체,

그리고 중간 경로를 왼쪽?

그런 다음 입력 할 때마다 프로그램이 오류를 발생시킵니다. 나는 이유를 모른다.

이 게임을 도와주세요.

대단히 고마워요. :-)

+1

어떤 식으로 제대로 작동하지 않습니까? 오류 메시지가 나타 납니까? 교수형이야? 그것은 일반적인 신체 상해를 일으키고 죽은자를 기르는 것입니까? –

+0

아, 네. 오래된'cause ('mayhem')'과'dead.raise (') 상황. 일어날 때가 싫어. –

답변

0

첫째, 질문 할 때 오류 메시지를 포함해야합니다.

Traceback (most recent call last): 
    File "game.py", line 174, in <module> 
    start() 
    File "game.py", line 159, in start 
    cthulhu_room() 
    File "game.py", line 120, in cthulhu_room 
    cthulhu_room() 
    File "game.py", line 104, in cthulhu_room 
    start() 
    File "game.py", line 159, in start 
    cthulhu_room() 
    File "game.py", line 104, in cthulhu_room 
    start() 
    File "game.py", line 157, in start 
    bear_room() 
    File "game.py", line 66, in bear_room 
    if haunted: 
NameError: global name 'haunted' is not defined 

이것은 Cthulhu를 떠난 직후에 발생한 오류입니다.

여기서 문제는 hauntedexchange_path 함수 내에 정의되어 있다는 것입니다. 문제는 함수 내부에서 정의 된 변수가 해당 함수에 대해 로컬이므로 다른 함수 내부에서 사용할 수 없다는 점입니다. 이렇게하면 두 개의 unqiue 변수가 생깁니다 (같은 이름 임에도 불구하고).

간단한 해결책은 (권장하지 않지만) 변수 haunted 실제로 것을 파이썬에게 키워드 global를 사용하여 다음 asked_for_helptimes_in_exchange_path과 함께 스크립트의 매우 시작할 때, 가장 외부 범위에 haunted을 정의하는 것입니다 지역 변수가 아닌 전역 변수.

일반적으로 전역 변수는 사용하지 말고 대신 매개 변수로 변수를 전달해야합니다.

관련 문제