def board10():
from random import randint
coins = 0
board = []
charac = []
for i in range(10):
row = []
for j in range(10):
row.append('O')
charac[x][y] = 'x'
board.append(row)
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Treasure Hunt!")
print_board(board)
print ("Total Coins:", coins)
def random_row1(board):
return randint(O, len(board) - 1)
def random_row2(board):
return randint(O, len(board) - 1)
def random_col1(board):
return randint(O, len(board[0]) - 1)
def random_col2(board):
return randint(O, len(board[0]) - 1)
left_across1 = random_row1(board)
right_across1 = random_row2(board)
up_vertical1 = random_col1(board)
down_vetical1 = random_col2(board)
for turn in range(10):
left_across2 = int(input("How many moves LEFT of the grid would you like to go?:"))
right_across2 = int(input("How many moves RIGHT of the grid would you like to go?:"))
up_vertical2 = int(input("How many moves UP of the grid would you like to go?:"))
down_vertical2 = int(input("How many moves DOWN of the grid would you like to go?:"))
if left_across2 == left_across1 and right_across2 == right_across1 and up_vertical2 == up_vertical1 and down_vertical2 == down_vertical1:
print ("Congratulations! You landed on a Treasure Chest!")
coins + 10
break
else:
if (left_across2 < 0 or left_across2 > 8) or (right_across2 < 0 or right_across2 > 8) or (up_vertical2 < 0 or up_vertical2 > 8) or (down_vertical2 < 0 or down_vertical2 > 8):
print ("Oops, that's not even in the grid. Try Again")
else:
print ("Turn", turn + 1 )
print_board(board)
print ("Total Coins:", coins)
choice = "";
while loop ==1:
print ("Menu")
print ("a.) Play the Game")
print ("b.) Quit the Game")
print ("")
choice = input("Select an option = ")
loop =0
if choice == 'a':
print("You have selected to Play the Game")
print("Select which size grid you would like to play")
print("1.) 8 x 8")
print("2.) 10 x 10")
print("3.) 12 x 12")
choice=input("Select an option = ")
if choice =='1':
board8()
elif choice == '2':
board10()
elif choice == '3':
board12()
else:
print("You've picked an invalid choice")
loop ==1
elif choice == 'b':
print("You have selected to Quit the Game")
quit()
else:
print("You've Picked an invalid Choice")
loop==1
我目前正在尝试制作一个宝岛游戏,其中代码打印出一个10x10(试图让这个首先工作,然后实现其余大小的地图),地图左下角有一个X.然后,用户告诉程序它想要向上,向左,向右和向下移动角色多少个位置然后移动。
它还应该随机隐藏硬币以允许角色获得积分。
目前我无法通过电路板上的X打印电路板。现在它返回错误
Traceback (most recent call last):
File "*file Root*/Controlled Assessment (1).py", line 91, in <module>
board10()
File "*file Root*/Controlled Assessment (1).py", line 17, in board10
charac[x][y] = 'x'
NameError: name 'x' is not defined
非常感谢任何帮助!
答案 0 :(得分:1)
这来自
行charac[x][y] = 'x'
^
当您为列表编制索引时,正如您在此处所做的那样,您应该使用评估为数字的内容。例如,如果您有一个名为lst
的变量,其中包含值[1, 2, 3, 4, 5]
(包含5个数字的列表),则lst[0]
为1
,lst[3]
为4
,等等。除了使用文字编号之外,您还可以使用包含数字的变量,例如foo
被定义为2
(您可以使用代码语句foo = 2
执行),然后lst[foo]
是3
。这是您在代码中尝试执行的操作,使用x
下存储的值来索引列表charac
。但是,您实际上从未在x
中添加数字,因此Python不知道如何处理它。这就是您收到此错误的原因。
重现此错误的一个非常简单的程序是
lst = [1, 2, 3, 4, 5]
print(lst[x])
修复此程序的一种简单方法是将其更改为以下内容:
lst = [1, 2, 3, 4, 5]
x = 2
print(lst[x])
将来,如果您尝试将程序缩减到可能出现错误的最小示例,就像我刚刚展示的那样,您将很容易找到许多错误。