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

コードはANと(この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' 
    ^

から来ています。たとえば、値[1, 2, 3, 4, 5](5つの数字のリスト)を保持するlstという変数がある場合は、lst[0]1,lst[3]4となります。リテラル番号を使用することに加えて、foo2(コード文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 ボード私のために= [] CHARAC = [0,1,2,3,4,5] を範囲内(10): 行範囲内のjに対する= [] (10): row.append( 'O') CHARAC [1] [1] = 'X' board.append(行) エラー: ファイル "* File Root */Controlled Assessment(1).py"、行91、 board10() ファイル "*ファイルルート* /制御評価(1)。"int"オブジェクトは項目割り当てをサポートしていません –

+0

また、あなたからのサポートを得ることができる電子メールがありますか? –

関連する問題