-2

到目前为止,我能够到达所有房间,除了从战斗到楼梯。我得到了要显示的楼梯的描述,但是当我尝试给它下一个方向时,什么都没有被识别出来。

继承人的代码:

def playGame():
   location = "Porch"
   showIntroduction()
   while not (location == "Exit"):
       showRoom(location)
       direction = requestString("Which direction")
       printNow("You typed: " + direction)
       location = pickRoom(direction, location)

# The instructions for the game      
def showIntroduction():
  printNow ("Welcome to the Adventure House!")
  printNow ("In each room, you will be told which directions you can go.")
  printNow ("You can move north, south, east, or west by typing")
  printNow ("that direction into the box.")
  printNow ("you can also pick up items that you will need on your journey")
  printNow ("They will present themselves in the text, to pick them up simplily type    there name")
  printNow ("Type help to replay this introduction.")
  printNow ("Type quit or exit to end the program.")

#This is called in the global defination of Inventory , makes and array
def createInventory():
   inv = range(11)
   inv[10] = "made"
   return inv

 # Main Inventory
 Inventory = createInventory()

 # Populating given inventory aray with items to be used throughout the game.
 def createItems(inv):
   items = inv
   items[1] = "Axe"
   items[2] = "Gas"
   items[3] = "keys"
   items[4] = "gun"
   items[5] = "note"
   items[9] = "full" 

# Check if the item passed to it is still in the inventory array
def checkItems(item):
    items = Inventory
    for i in range(len(items)):
        if items[i] == item:
            return "yes"
        return "no"



 #Put an item in the inventory (Key words : grab /  item name)
 def stockInventory(item):
    inv = Inventory
    for i in range (11):
       if inv[i] == 0:
          inv[i] = item
          break
    return inv

 #Check for an item to make text appear.
 def checkInventory(item):
    inv = Inventory
    for i in range(0, 11):
        if item == inv[i]:
           return "yes"
        return "no"


# A helper function that calls specific room description 
# functions based on the value of the parameter room 
def showRoom(room):
   printNow("============")
   if room == "Porch":
      showPorch()
   elif room == "Entryway":
      showEntryway()
   elif room == "Kitchen":
      showKitchen()
   elif room == "LivingRoom":
      showLR()
   elif room == "DiningRoom":
      showDR()
   elif room == "Stairs" :
      showStairs()
   elif room == "Nurcery":
      showNurcery()
   elif room == "Hole":
      showHoleFalling()
   elif room == "Basement":
      showBasement()
  elif room == "BasementW":
    showBasementW()
  elif room == "BasementN":
     showBasementN()
  elif room == "BasementS":
     showBasementS()
  elif room == "BasementE":
     showBasementE()
  elif room == "frontYardFight":
      showFight()
  elif room == "Car":
     showCar()
  elif room == "win":
     Winning()
  elif room == "Fight":
     showFight1()
  # Add rooms above this line!
  else:  #This clause should never execute
       printNow("I don't recognize the room")

  # This function specifies what room is entered based on the current room
  # and the direction requested by the user.
def pickRoom (direction, room):
if (direction == "quit"  or direction == "exit"):
    printNow("Goodbye!")
    return "Exit"
elif direction == "help" or direction == "Help":
    showIntroduction()
    return room

elif room == "Porch":
    if direction == "north":
       return "Entryway"
    elif direction == "truck":
        printNow ("The truck is out of gas and your keys seem to be missing. you return to the porch")
    else:
       printNow("There's no way to go that direction.")
       return room

elif room == "Entryway":
    if direction == "north":
       return "Kitchen"
    elif direction == "south":
       return "Porch"
    elif direction == "east":
       return "LivingRoom"
    elif direction == "read":
        printNow ("This house will be your doom")
    else:
       printNow ("The is not the correct word")
       return room

elif room == "Kitchen":
    if direction == "south":
       return "Entryway"
    elif direction == "east":
       return "DiningRoom"
    elif direction == "west":
        answer = checkInventory("Axe")
        if answer == "yes":
            printNow (" You feel the urge to get shinning  on this door, you decide to swing your axe and break it")
            return "Fight"
        else:
            printNow (" the door is locked, find a a way to open it.")
            return room




elif room == "Fight":
  answer = checkInventory("Axe")
  if direction ==  "swing":
       printNow ("You landed your blow! it's down!")
       return "Stairs"
  else:
       printNow(" wrong keyword ")
       return room
  return "Stairs"

elif room == "LivingRoom":
    if direction == "north":
       return "DiningRoom"
    elif direction == "west":
       return "Entryway"
    elif direction == ("axe" or "Axe"):
       stockInventory("Axe")
       printNow(" You grabbed the Axe ")
       return room
    else:
       printNow ("You have the wrong keyword.")
       return room

elif room == "DiningRoom":
    if direction == "south":
       return "LivingRoom"
    elif direction == "west":
       return "Kitchen"
    elif direction == ("Gas" or "gas"):
       stockInventory("Gas")
       printNow (" you grabbed the gas ")
    else:
       printNow ("You have the wrong keyword.")
       return room

elif room == "Stairs":
     if direction == "up":
        return "TopHall"
     elif direction == ("keys" or "Keys"):
        stockInventory("Keys")
        printNow (" You got the Keys!")
     else:
        printNow ("You have the wrong keyword.")
        return room

elif room == "TopHall":
    if direction == "north":
       return "Hole"
    elif direction == "west":
        return "Nurcery"
    else:
        printNow(" You have the wrong keyword. ")
        return room

elif room == "Nurcery":
    if direction == "west":
       return "TopHall"
    else:
       printNow(" You have the wrong keyword. ")
       return room

elif room == "Hole":
   return "Basement"

elif room == "Basement":
   if direction == "west":
      return "BasementW"
   elif direction == "north":
      return "BasementN"
   elif direction == "south":
      return "BasementS"
   elif direction == "east":
      return "BasementE"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "BasementW":
   if direction == "east":
      return "Basement"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "BasementN":
   if direction == "south":
      return "Basement"
   elif direction == "wall":
      return "FrontYard"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "basementS":
   if direction == "north":
     return "Basement"
   elif direction == ("gun" or "Gun"):
     stockInventory("gun") 
     printNow (" you got the Gun!")
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "BasementE":
   if direction == "west":
     return "Basement"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "frontYardFight":
   if direction == "shoot":
       answer = checkInventory("gun")
       if answer == "yes" or "Yes":
           printNow(" You shoot the monster straight in the head" )
           return "Car"
       else:
          return "quit"
   else:
     printNow(" That is the wrong word ")
     return room


elif room == "Car":
   answer1 = checkInventory("Keys")
   answer2 = checkInventory("Gas")
   if answer1 and answer2 == "Yes" or "yes":
       return "win"
   else:
       printNow("your missing something")
       return "Porch"




#add rooms above this line!
else:  # this clause should never execute!
    printNow("An undefined room has been detected.")
    return "Porch"


######  a group of functions that print room descriptions    

def showPorch():
   printNow("You wake from a deep slumber, you're lieing on the porch of an abandonded house")
   printNow("The windows are broken.  It's a dark and stormy night")
   printNow("Your truck is sitting in the parking lot, when you examine it, it is out of gas and your keys are gone")
   printNow("You can go north into the house if you dare.")  

等等

4

2 回答 2

0

我在这里进行了疯狂的猜测,但我猜这个错误是您担心的错误:

 elif direction == ("keys" or "Keys"):

这并不像你认为的那样。("keys" or "Keys")和 是一样的"keys"。所以,这与elif direction == "keys":.

您想要其中之一:

elif direction == "keys" or direction == "Keys":
elif direction in ("keys", "Keys"):
elif direction.lower() == "keys":

或者,更好的是,更改playGame为 call pickRoom(direction.lower(), location),然后整个过程pickRoom都可以与小写字符串进行比较。

你做对了很多次,就像这里:

elif direction == "help" or direction == "Help":

……但你在其他地方也弄错了,你必须把它们都修好。

于 2013-11-13T00:48:14.423 回答
0

一个问题是,在函数pickRoom中,并非所有 long 的分支都if...elif...else返回一个值。在接下来的部分中,如果用户键入“keys”,则不会返回任何房间,程序应输出“检测到未定义的房间”。在随后的循环中。

elif room == "Stairs":
    if direction == "up":
        return "TopHall"
    elif direction == ("keys" or "Keys"):
        stockInventory("Keys")
        printNow (" You got the Keys!")
    else:
        printNow ("You have the wrong keyword.")
        return room

处理此问题的一种简单方法是return room在函数末尾添加,以处理之前未返回的所有剩余情况。

于 2013-11-13T01:04:21.970 回答