列表和字典语法错误

2024-09-28 23:02:39 发布

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

所有人!我有一个垄断模拟器,是运行到一个错误。我认为这是一个语法错误,但我不能找出正确的语法。这是我的密码:

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])

当我尝试运行程序时,错误: “回溯(最近一次呼叫): 文件“python”,第81行,in TypeError:不可损坏的类型:“list”(列表)总是弹出。 谢谢大家!你知道吗


Tags: ofthecommunityboardnumberpositionrandominteger
1条回答
网友
1楼 · 发布于 2024-09-28 23:02:39

我想我了解你在做什么,并且已经测试了你的代码的一个工作版本。你知道吗

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

  1. 字典值中列表索引的语法

BoardLands[(Board[P1_Position]), [1]] = BoardLands[(Board[P1_Position]), [1]] + 1

我可以看到你在运行这个代码时试图更新方块的命中率。我不想细说,但这种方式是错误的,原因很多。访问该列表项的正确方法是BoardLands[Board[P1_Position]][0],而将+ 1添加到列表项的更好方法是BoardLands[Board[P1_Position]][0] += 1

  1. 对象类型

您的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])

相关问题 更多 >