列表和词典的语法错误

时间:2017-07-26 14:07:10

标签: python syntax

大家好!我有一个遇到错误的Monopoly Simulator。我认为这是一个语法错误,但是,我无法弄清楚正确的语法。这是我的代码:

from random import randint

P1_Money=1640
#Sets inital money of player 1

P1_Position=0
#Sets initial position of both players

Jail_Number=0
#Sets Jail_Number which decides if the player can move out of jail

Board = ['Go', 'Mediterranean Avenue', 'Community Chest 1', 'Baltic Avenue', 'Income Tax', 'Reading Railroad', 'Oriental Avenue', 'Chance 1', 'Vermont Avenue', 'Connecticut Avenue', 'Jail', 'St. Charles Place', 'Electric Company', 'States Avenue', 'Virginia Avenue', 'Pennsylvania Railroad', 'St. James Place', 'Community Chest 2', 'Tenessee Avenue', 'New York Avenue', 'Free Parking', 'Kentucky Avenue', 'Chance 2', 'Indiana Avenue', 'Illinois Avenue', 'B. & O. Railroad', 'Atlantic Avenue', 'Ventnor Avenue', 'Water Works', 'Marvin Gardens', 'Go To Jail', 'Pacific Avenue', 'North Carolina Avenue', 'Community Chest 3', 'Pennsylvania Avenue', 'Short Line', 'Chance 3', 'Park Place', 'Luxury Tax', 'Boardwalk']
#List of all of the squares on the board

BoardLands = {'Go':[0,-200], 'Mediterranean Avenue': [0,2], 'Community Chest 1':0, 'Baltic Avenue':[0,4], 'Income Tax':[0,200], 'Reading Railroad':[0,200], 'Oriental Avenue':[0,6], 'Chance 1':[0,0], 'Vermont Avenue':[0,6], 'Connecticut Avenue':[0,8], 'Jail':[0,0], 'St. Charles Place':[0,0], 'Electric Company':[0,0], 'States Avenue':[0,10], 'Virginia Avenue':[0,12], 'Pennsylvania Railroad':[0,200], 'St. James Place':[0,14], 'Community Chest 2':[0,0], 'Tenessee Avenue':[0,14], 'New York Avenue':[0,16], 'Free Parking':[0,0], 'Kentucky Avenue':[0,18], 'Chance 2':[0,0], 'Indiana Avenue':[0,18], 'Illinois Avenue':[0,20], 'B. & O. Railroad':[0,200], 'Atlantic Avenue':[0,22], 'Ventnor Avenue':[0,22], 'Water Works':[0,0], 'Marvin Gardens':[0,22], 'Go To Jail':[0,0], 'Pacific Avenue':[0,26], 'North Carolina Avenue':[0,26], 'Community Chest 3':[0,0], 'Pennsylvania Avenue':[0,28], 'Short Line':[0,200], 'Chance 3':[0,0], 'Park Place':[0,35], 'Luxury Tax':[0,200], 'Boardwalk':[0,50]}
print (BoardLands['Chance 1'][1])
#Example of value in dictionary: 'Made Up Name': [0,20]
#'Made up name' is the name of the square, the first value in the list is the number of lands it has recieved, and the second value in the list is how much money you recieve from landing on it, with the exception of Community Chests and Chance Squares, which are handled by their respective handlers.

#print (Board[2], Board[17], Board[33]) All of the Community Chests
#print (Board[7], Board[22], Board[36]) All of the Chances
i=0 #Sets initial value for Itteration

while (i<500):
  if Jail_Number==0:
    P1_Position = (P1_Position+randint(1,6)+randint(1,6))
    if P1_Position>39:
      P1_Position=P1_Position-40

  else:
    Die_1=randint(1,6)
    Die_2=randint(1,6)
    if Die_1==Die_2:
      P1_Position = (P1_Position+Die_1+Die_2)
      Jail_Number=0
      print ('Player moved out of jail!')
    else:
      Jail_Number=Jail_Number-1

  print('Board Location of Player One: ', Board[P1_Position])      
  #Displays what player landed on what square
  if P1_Position == 2 or P1_Position == 17 or P1_Position == 33:   
      Random_Integer = randint(1,17)
      if Random_Integer == 1:
        BoardLands[Board[P1_Position]] = BoardLands[Board[P1_Position]]+1   
        P1_Position = 0
        print('Community Chest Reads: Advance to Go')
        print('Player Moved to Go!')
      elif Random_Integer == 2:
        print('Bank error in your favor - collect $75')
      elif Random_Integer == 3:
        print('Doctor\'s fees - Pay $50')
      elif Random_Integer == 4:
        print('Get out of jail free! - This card may be kept until needed, or sold')
      elif Random_Integer == 5:
        BoardLands[Board[P1_Position]] = BoardLands[Board[P1_Position]]+1
        P1_Position = 10
        Jail_Number = 3
        print('Go to jail - go directly to jail - Do not pass Go, do not collect $200')
        print('Player Moved to Jail!')
      elif Random_Integer == 6:
        print('It\'s your birthday! Collect $10 from each player')
      elif Random_Integer == 7:
        print('Grand Opera Night - collect $50 from every player for opening night seats')
      elif Random_Integer == 8:
        print('Income Tax Refund - Collect $200')
      elif Random_Integer == 9:
        print('Life Insurance Matures - Collect $100')
      elif Random_Integer == 10:
        print('Pay Hospital Fees of $100')
      else:
        print('I\'m lazy')
    #Community Chest Handler
  if P1_Position == 30:
    P1_Position = 10
    Jail_Number = 3
    BoardLands[Board[30],[(1)]] = BoardLands[Board[30],[(1)]]+1
    print("Player moved to jail")
  if P1_Position == 12 or P1_Position == 27:   #Utilities Handler
    P1_Money=P1_Money-((randint(1,6)+randint(1,6))*4)
  BoardLands[(Board[P1_Position]),[1]] = BoardLands[(Board[P1_Position]),[1]]+1
  #Updates statistics collector on what square was landed on
  i=i+1
  #Updates Itteration

print(" ")
print(" ")
print("Board Lands Stats")
for square in BoardLands:
  print (" ")
  print (square,  ":", BoardLands[square])

当我尝试运行程序时,错误: &#34; Traceback(最近一次调用最后一次):   文件&#34; python&#34;,第81行,in TypeError:不可用类型:&#39; list&#39;&#34;总是弹出来。 谢谢大家!

1 个答案:

答案 0 :(得分:0)

我想我明白你在做什么,并测试了你的代码的工作版本。

首先,@ asongtoruin是正确的,引用BoardLands[Board[P1_Position]]时使用的语法导致错误有两个原因。

  1. 字典值中的列表索引语法
  2. BoardLands[(Board[P1_Position]), [1]] = BoardLands[(Board[P1_Position]), [1]] + 1

    我可以看到你在运行此代码时尝试更新方块的命中数。我不会详细介绍,但出于多种原因,这种方式是错误的。访问该列表项的正确方法是BoardLands[Board[P1_Position]][0],只需将+ 1添加到列表项的更好方法是BoardLands[Board[P1_Position]][0] += 1

    1. 对象类型
    2. 您的BoardLands字典包含键值对,其值可以是:

      • list() | 'Mediterranean Avenue': [0, 2]
      • int() | 'Community Chest 1': 0

      您需要在整个代码中考虑这两种情况。我在添加之前添加了if语句来检查对象类型。

      if isinstance(BoardLands[Board[P1_Position]], type(list())):
          BoardLands[Board[P1_Position]][0] += 1
      elif isinstance(BoardLands[Board[P1_Position]], type(int())):
          BoardLands[Board[P1_Position]] += 1
      

      这是我在调整这几件事之后开始工作的代码。

      from random import randint
      
      P1_Money = 1640
      # Sets inital money of player 1
      
      P1_Position = 0
      # Sets initial position of both players
      
      Jail_Number = 0
      # Sets Jail_Number which decides if the player can move out of jail
      
      Board = ['Go', 'Mediterranean Avenue', 'Community Chest 1', 'Baltic Avenue', 'Income Tax', 'Reading Railroad',
               'Oriental Avenue', 'Chance 1', 'Vermont Avenue', 'Connecticut Avenue', 'Jail', 'St. Charles Place',
               'Electric Company', 'States Avenue', 'Virginia Avenue', 'Pennsylvania Railroad', 'St. James Place',
               'Community Chest 2', 'Tenessee Avenue', 'New York Avenue', 'Free Parking', 'Kentucky Avenue', 'Chance 2',
               'Indiana Avenue', 'Illinois Avenue', 'B. & O. Railroad', 'Atlantic Avenue', 'Ventnor Avenue', 'Water Works',
               'Marvin Gardens', 'Go To Jail', 'Pacific Avenue', 'North Carolina Avenue', 'Community Chest 3',
               'Pennsylvania Avenue', 'Short Line', 'Chance 3', 'Park Place', 'Luxury Tax', 'Boardwalk']
      # List of all of the squares on the board
      
      BoardLands = {'Go': [0, -200], 'Mediterranean Avenue': [0, 2], 'Community Chest 1': 0, 'Baltic Avenue': [0, 4],
                    'Income Tax': [0, 200], 'Reading Railroad': [0, 200], 'Oriental Avenue': [0, 6], 'Chance 1': [0, 0],
                    'Vermont Avenue': [0, 6], 'Connecticut Avenue': [0, 8], 'Jail': [0, 0], 'St. Charles Place': [0, 0],
                    'Electric Company': [0, 0], 'States Avenue': [0, 10], 'Virginia Avenue': [0, 12],
                    'Pennsylvania Railroad': [0, 200], 'St. James Place': [0, 14], 'Community Chest 2': [0, 0],
                    'Tenessee Avenue': [0, 14], 'New York Avenue': [0, 16], 'Free Parking': [0, 0],
                    'Kentucky Avenue': [0, 18], 'Chance 2': [0, 0], 'Indiana Avenue': [0, 18], 'Illinois Avenue': [0, 20],
                    'B. & O. Railroad': [0, 200], 'Atlantic Avenue': [0, 22], 'Ventnor Avenue': [0, 22],
                    'Water Works': [0, 0], 'Marvin Gardens': [0, 22], 'Go To Jail': [0, 0], 'Pacific Avenue': [0, 26],
                    'North Carolina Avenue': [0, 26], 'Community Chest 3': [0, 0], 'Pennsylvania Avenue': [0, 28],
                    'Short Line': [0, 200], 'Chance 3': [0, 0], 'Park Place': [0, 35], 'Luxury Tax': [0, 200],
                    'Boardwalk': [0, 50]}
      print(BoardLands['Chance 1'][1])
      # Example of value in dictionary: 'Made Up Name': [0,20]
      # 'Made up name' is the name of the square, the first value in the list is the number of lands it has recieved, and the second value in the list is how much money you recieve from landing on it, with the exception of Community Chests and Chance Squares, which are handled by their respective handlers.
      
      # print (Board[2], Board[17], Board[33]) All of the Community Chests
      # print (Board[7], Board[22], Board[36]) All of the Chances
      i = 0  # Sets initial value for Itteration
      
      while (i < 500):
          if Jail_Number == 0:
              P1_Position = (P1_Position + randint(1, 6) + randint(1, 6))
              if P1_Position > 39:
                  P1_Position = P1_Position - 40
      
          else:
              Die_1 = randint(1, 6)
              Die_2 = randint(1, 6)
              if Die_1 == Die_2:
                  P1_Position = (P1_Position + Die_1 + Die_2)
                  Jail_Number = 0
                  print('Player moved out of jail!')
              else:
                  Jail_Number = Jail_Number - 1
      
          print('Board Location of Player One: ', Board[P1_Position])
          # Displays what player landed on what square
          if P1_Position == 2 or P1_Position == 17 or P1_Position == 33:
              Random_Integer = randint(1, 17)
              if Random_Integer == 1:
                  if isinstance(BoardLands[Board[P1_Position]], type(list())):
                      BoardLands[Board[P1_Position]][0] += 1
                  elif isinstance(BoardLands[Board[P1_Position]], type(int())):
                      BoardLands[Board[P1_Position]] += 1
                  P1_Position = 0
                  print('Community Chest Reads: Advance to Go')
                  print('Player Moved to Go!')
              elif Random_Integer == 2:
                  print('Bank error in your favor - collect $75')
              elif Random_Integer == 3:
                  print('Doctor\'s fees - Pay $50')
              elif Random_Integer == 4:
                  print('Get out of jail free! - This card may be kept until needed, or sold')
              elif Random_Integer == 5:
                  if isinstance(BoardLands[Board[P1_Position]], type(list())):
                      BoardLands[Board[P1_Position]][0] += 1
                  elif isinstance(BoardLands[Board[P1_Position]], type(int())):
                      BoardLands[Board[P1_Position]] += 1
                  P1_Position = 10
                  Jail_Number = 3
                  print('Go to jail - go directly to jail - Do not pass Go, do not collect $200')
                  print('Player Moved to Jail!')
              elif Random_Integer == 6:
                  print('It\'s your birthday! Collect $10 from each player')
              elif Random_Integer == 7:
                  print('Grand Opera Night - collect $50 from every player for opening night seats')
              elif Random_Integer == 8:
                  print('Income Tax Refund - Collect $200')
              elif Random_Integer == 9:
                  print('Life Insurance Matures - Collect $100')
              elif Random_Integer == 10:
                  print('Pay Hospital Fees of $100')
              else:
                  print('I\'m lazy')
                  # Community Chest Handler
          if P1_Position == 30:
              P1_Position = 10
              Jail_Number = 3
              BoardLands[Board[30]][0] += 1
              print("Player moved to jail")
          if P1_Position == 12 or P1_Position == 27:  # Utilities Handler
              P1_Money = P1_Money - ((randint(1, 6) + randint(1, 6)) * 4)
      
          if isinstance(BoardLands[Board[P1_Position]], type(list())):
              BoardLands[Board[P1_Position]][0] += 1
          elif isinstance(BoardLands[Board[P1_Position]], type(int())):
              BoardLands[Board[P1_Position]] += 1
          # Updates statistics collector on what square was landed on
          i = i + 1
          # Updates Itteration
      
      print(" ")
      print(" ")
      print("Board Lands Stats")
      for square in BoardLands:
          print(" ")
          print(square, ":", BoardLands[square])