2017-02-21 21 views
-1
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 

目前我试图让宝岛的游戏,其中的代码打印出10×10与(试图让这个先工作,然后实现大小地图的其余部分) 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 

任何帮助将不胜感激!

回答

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

在未来,如果你尝试把你的程序减少到最小的可能的错误示例,就像我刚刚展示的那样,你将很容易找到你得到的许多错误。

+0

感谢大卫的回应,非常感谢。感谢您的编辑!回答了我的问题。 –

+0

你现在可以看看吗。当我尝试运行我得到这个错误代码: 代码: 从随机进口randint 币= 0 板对于i = [] CHARAC = [0,1,2,3,4,5] 在范围(10): 行= [] 在范围Ĵ(10): row.append( 'O') CHARAC [1] [1] = 'X' board.append(行) 错误: 文件“*文件根* /受控评估(1).py”,第91行,在 board10() 文件“*文件根* /受控评估(1)。py“,第17行,在board10 charac [1] [1] ='X' TypeError:'int'对象不支持项目分配 –

+0

或者您是否有电子邮件我可能会从您那里获得一些支持? –