获取不正确的输入

2024-04-20 00:35:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这一点还不熟悉,但我要求对这个学校项目提供一些意见。 任何帮助都可以。 我正在创造房间之间的移动和收集物品来杀死一个外星人。 我曾尝试运行代码,但它给了我一个错误的代码,我改变了一些东西,仍然是相同的代码

这是“代码”:

def input_instructions(item):
   # Printing input instructions
   print('Contain the Asset (Alien) Game')
   print('Gather 7 items when landing at the objective')
   print('Move to different locations on the map to gather the items: go 
   South, go East, go West, go North')
   print("Add to Inventory: collect 'item'", item)
   while True:
       asked = input('Enter: GO <direction> or GET <item>: ').split()
       if asked[0].upper() == "GO":
          asked[1] = asked[1].capitalize()
          if asked[1][0] == 'North' or asked[1][0] == 'South' or asked[1
          [0] 
          == 'East' or asked[1][0] == 'West':
             return asked[1]
          else:
              continue
       elif asked[0].upper() == 'GET':
          if asked[1].upper() == item.upper():
             return item
       else:
          continue

# show the current status
def input_status(currentLocation, inventory, currentItem):
    # print the players current location
    print('Your current location is: ', currentLocation)
    # print the players inventory options
    print('Your inventory so far is: ', inventory)
    # print an item if there is one
    print('Your current item is: ', currentItem)
    print()

# print out the locations
locations = {
    'Transport Rover': {'North': 'Watch Tower', 'South': 'Landing Site1', 
    'East': 
    'Weapon Storage Facility', 'West': 'Launch Site'},
    'Watch Tower': {'North': 'Transport Rover', 'East': 'Storage Room', 
    'item': 
    'Key Card'},
    'Storage Room': {'West': 'Watch Tower', 'item': 'Flashlight'},
    'Landing Site1': {'South': 'Transport Rover', 'East': 'Landing Site2', 
    'item': 
    'Transport Cage'},
    'Landing Site2': {'East': 'Landing Site1', 'item': 'Energy Cells'},
    'Testing Facility': {'West': 'Landing Site1', 'East': 'Testing Facility', 
    'item': 'Asset(Alien)'}, # Creature
    'Weapon Storage Facility': {'East': 'Transport Rover', 'North': 'Medical 
    Wing', 'item': 'Laser Guns'},
    'Medical Wing': {'South': 'Weapon Storage Facility', 'item': 'Medical 
    Supplies'},
    'Launch Site': {'West': 'Transport Rover', 'item': 'Ammo'}
}
# print out the what each location has
storage = {
    'Watch Tower': 'Key Card',
    'Storage Room': 'Flashlight',
    'Landing Site1': 'Transport Cage',
    'Landing Site2': 'Energy Cells',
    'Testing Facility': 'Asset(Alien)',
    'Weapon Storage Facility': 'Laser Guns',
    'Medical Wing': 'Medical Supplies',
    'Launch Site': 'Ammo'
}
# input your current location
where = 'Transport Rover'
what = []

while True:
    input_status(where, what, storage[where])
    ans = input_instructions(storage[where])
    if ans == storage[where]:
        what.append(ans)
        continue
    else:
        if ans in locations[where]:
            where = locations[where][ans]
            if where == 'Testing Facility':
               print('You are now fighting the Alien...')
               if len(what) > 3:
                  print('...')
                  print('Mission accomplish you and your team have capture 
                  the Asset(Alien)!')
                else:
                  print('...')
                  print('Mission Failed')
                break

          else:
              print()`enter code here'
              print('')
              continue

Tags: theinputifstorageitemwheretransportasked
1条回答
网友
1楼 · 发布于 2024-04-20 00:35:36

首先,您的存储中没有Transport Rover,而是在locations中。所以我猜你是指location[where]而不是storage[where]。还有一些缩进错误。我修正了一些,让我知道这是否是你想要的。 代码如下:

def input_instructions(item):
   # Printing input instructions
   print('Contain the Asset (Alien) Game')
   print('Gather 7 items when landing at the objective')
   print('Move to different locations on the map to gather the items: go South, go East, go West, go North')
   print("Add to Inventory: collect 'item'", item)
   while True:
       asked = input('Enter: GO <direction> or GET <item>: ').split()
       if asked[0].upper() == "GO":
          asked[1] = asked[1].capitalize()
          if asked[1][0] == 'North' or asked[1][0] == 'South' or asked[1][0] == 'East' or asked[1][0] == 'West':
             return asked[1]
          else:
              continue
       elif asked[0].upper() == 'GET':
          if asked[1].upper() == item.upper():
             return item
       else:
          continue

# show the current status
def input_status(currentLocation, inventory, currentItem):
    # print the players current location
    print('Your current location is: ', currentLocation)
    # print the players inventory options
    print('Your inventory so far is: ', inventory)
    # print an item if there is one
    print('Your current item is: ', currentItem)
    print()

# print out the locations
locations = {
    'Transport Rover': {'North': 'Watch Tower', 'South': 'Landing Site1', 
    'East': 
    'Weapon Storage Facility', 'West': 'Launch Site'},
    'Watch Tower': {'North': 'Transport Rover', 'East': 'Storage Room', 
    'item': 
    'Key Card'},
    'Storage Room': {'West': 'Watch Tower', 'item': 'Flashlight'},
    'Landing Site1': {'South': 'Transport Rover', 'East': 'Landing Site2', 
    'item': 
    'Transport Cage'},
    'Landing Site2': {'East': 'Landing Site1', 'item': 'Energy Cells'},
    'Testing Facility': {'West': 'Landing Site1', 'East': 'Testing Facility', 
    'item': 'Asset(Alien)'}, # Creature
    'Weapon Storage Facility': {'East': 'Transport Rover', 'North': 'Medical Wing', 'item': 'Laser Guns'},
    'Medical Wing': {'South': 'Weapon Storage Facility', 'item': 'Medical Supplies'},
    'Launch Site': {'West': 'Transport Rover', 'item': 'Ammo'}
}
# print out the what each location has
storage = {
    'Watch Tower': 'Key Card',
    'Storage Room': 'Flashlight',
    'Landing Site1': 'Transport Cage',
    'Landing Site2': 'Energy Cells',
    'Testing Facility': 'Asset(Alien)',
    'Weapon Storage Facility': 'Laser Guns',
    'Medical Wing': 'Medical Supplies',
    'Launch Site': 'Ammo',
}
# input your current location
where = 'Transport Rover'
what = []

while True:
    input_status(where, what, locations[where])
    ans = input_instructions(locations[where])
    if ans == locations[where]:
        what.append(ans)
        continue
    else:
        if ans in locations[where]:
            where = locations[where][ans]
            if where == 'Testing Facility':
                print('You are now fighting the Alien...')
                if len(what) > 3:
                    print('...')
                    print('Mission accomplish you and your team have capture the Asset(Alien)!')
                else:
                    print('...')
                    print('Mission Failed')
                break

            else:
                print('pass')

相关问题 更多 >