我是编码新手,我的井字游戏出现错误

时间:2019-07-16 22:53:14

标签: python-3.x 2d-games

我当时正在使用视频制作井字游戏,所以我承认这并不是我真正在做的游戏,但对编码来说是非常新的东西,这是我想学习的方式。我在使用Repl.it.当我完成代码后,我最终运行它进行测试,自己玩了一个游戏,然后向我的朋友展示,在显示了一些代码之后,我们尝试播放它,然后我出错了,但我发誓我没有改变任何东西。错误基本上显示为“ Typeerror:-:'str'和'int'的不受支持的操作符类型”

它说我将在代码段中放入170、34和60行的错误 这是整个项目的链接---> https://repl.it/@JoshMayes/Tic-Tac-Toe

我没有做太多尝试,对此我表示歉意,因为我对编码的过度经验不足,我认为如果直接去找专家可能会更好

   def play_game():

     #Display the initial board
     display_board()

     while game_still_in_progress:

       #Go through the turn procedure for the current player
       handle_turn(current_player) <----- line 34

       #check if the game has ended
       check_if_game_over()

       #Flip to the other player
       flip_player()
     # The game has ended
     if winner == "X" or winner == "O":
       print(winner + " player won!")
     elif winner == None:
       print("Cat's game!")




    while not valid:

       while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
        position + input("Invalid input. Choose a position from 1-9: ")

       position = int(position - 1)  <-------Line 60

       if board [position] == "-":
         valid = True
       else:
         print("Invalid Placement. Position Taken. Choose another Position")


      play_game() <---- line 170

顺便说一句链接到这里的整个事情---> https://repl.it/@JoshMayes/Tic-Tac-Toe

我希望游戏能够正常运行,因为它可以在出现错误的5分钟前运行,而且我发誓我什么都没做,只能重新加载页面,因为有一条消息告诉我要这样做。但是,当尝试第二次播放时,它不起作用。

1 个答案:

答案 0 :(得分:2)

更新这2行

position = input("Invalid input. Choose a position from 1-9: ")
position = int(position) - 1  <-------Line 60