2017-02-21 22 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

或者您是否有電子郵件我可能會從您那裏獲得一些支持? –