获取“TypeError:并非在字符串格式化期间转换所有参数”,并不确定原因

时间:2018-02-26 07:33:53

标签: python

我正在研究一个关于学习Python的Udemy课程的项目。我的目标是创建一个tic tac toe游戏,每当我运行游戏时出于某种原因运行游戏,当我选择将任何一个标记放在棋盘上时,我会收到错误:

Traceback (most recent call last):
File "C:\Users\zanec\Desktop\Udemy_Python_course\practice\milestone-1-tic-
tac-toe-game.py", line 189, in <module>
player_input()
File "C:\Users\zanec\Desktop\Udemy_Python_course\practice\milestone-1-tic-
tac-toe-game.py", line 137, in player_input
Player_1_lucky()
File "C:\Users\zanec\Desktop\Udemy_Python_course\practice\milestone-1-tic-
tac-toe-game.py", line 36, in Player_1_lucky
if number%2 != 0:
TypeError: not all arguments converted during string formatting

这是代码。任何反馈我将不胜感谢。

import random
from IPython.display import clear_output

#tries = 1
board = [0,1,2,3,4,5,6,7,8,9]
player_one = ('X')
player_two = ('O')

def display_board(board):

    print('     |     | ')
    print(' ',board[7],' | ',board[8],' | ',board[9])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[4],' | ',board[5],' | ',board[6])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[1],' | ',board[2],' | ',board[3])
    print('     |     | ')

def player_input():

    players_rand = 0 #random.randint(0,1)

    for number in board:

            if number == 0:
                print('Welcome to Tic Tac Toe!')

            else:
                if players_rand == 0:
                    def Player_1_lucky():

                        if number%2 != 0:
                            #def Player_1_turn():
                            answer1 = int(input('Player 1, assign the 
marker: X, to the board using a number from 1 to 9: '))
                            #answer_convert1 = int(answer1)

                            if answer1 == 1:
                                board[1] = ('X')
                                display_board(board)
                            #elif board[1] == ('X'):
                                #print('You cannot use this place because it 
is already taken. Please pick another number.')
                                #Player_1_turn()
                            if answer1 == 2:
                                board[2] = ('X')
                                display_board(board)
                            if answer1 == 3:
                                board[3] = ('X')
                                display_board(board)
                            if answer1 == 4:
                                board[4] = ('X')
                                display_board(board)
                            if answer1 == 5:
                                board[5] = ('X')
                                display_board(board)
                            if answer1 == 6:
                                board[6] = ('X')
                                display_board(board)
                            if answer1 == 7:
                                board[7] = ('X')
                                display_board(board)
                            if answer1 == 8:
                                board[8] = ('X')
                                display_board(board)
                            if answer1 == 9:
                                board[9] = ('X')
                                display_board(board)

                            #Player_1_turn()

                        elif number%2 == 0:
                            def Player_2_turn():
                                answer2 = int(input('Player 2, assign the 
marker: O, to the board using a number from 1 to 9: '))
                                #answer_convert2 = int(answer2)

                                if answer2 == 1 and board[1] == ('X'):
                                    #board[1] = ('X')
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 1:
                                    board[1] = ('O')
                                    display_board(board)
                                if answer2 == 2 and board[2] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 2:
                                    board[2] = ('O')
                                    display_board(board)
                                if answer2 == 3 and board[3] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 3:
                                    board[3] = ('O')
                                    display_board(board)
                                if answer2 == 4 and board[4] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 4:
                                    board[4] = ('O')
                                    display_board(board)
                                if answer2 == 5 and board[5] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 5:
                                    board[5] = ('O')
                                    display_board(board)
                                if answer2 == 6 and board[6] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 6:
                                    board[6] = ('O')
                                    display_board(board)
                                if answer2 == 7 and board[7] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 7:
                                    board[7] = ('O')
                                    display_board(board)
                                if answer2 == 8 and board[8] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 8:
                                    board[8] = ('O')
                                    display_board(board)
                                if answer2 == 9 and board[9] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 9:
                                    board[9] = ('O')
                                    display_board(board)

                            Player_2_turn()

                    Player_1_lucky()    

                if players_rand == 1:

                    if number%2 != board[0]:
                        answer2 = input('Play   er 2, assign the marker: O, 
 to the board using a number from 1 to 9: ')
                        answer_convert2 = int(answer2)
                    else:
                        answer1 = input('Player 1, assign the marker: X, to 
the board using a number from 1 to 9: ')
                        answer_convert1 = int(answer1)

display_board(board)
player_input()

3 个答案:

答案 0 :(得分:2)

在这些方面:

answer2 = int(input('Player 2, assign the marker: O, to the board using a number from 1 to 9: '))
#answer_convert2 = int(answer2)

代码正确地将用户的输入从字符串转换为int。但在这些方面降低了:

answer2 = input('Player 2, assign the marker: O, to the board using a number from 1 to 9: ')
answer_convert2 = int(answer2)

代码将answer2作为字符串。这意味着这一行:

if number % 2 != 0:

有时会执行您期望的模运算并测试奇数(当它是整数时),有时会尝试进行字符串插值(当它是一个字符串时)。

答案 1 :(得分:0)

正如Paulo Scardine指出的那样。问题是随着游戏的进行,数量值会发生变化。对此的一个简单修复可能是添加一个名为turn的附加参数,并使用它来跟踪玩家的转弯。

import random
from IPython.display import clear_output

#tries = 1
turns = range(10)
board = ["","","","","","","","","",""]
player_one = ('X')
player_two = ('O')

def display_board(board):

    print('     |     | ')
    print(' ',board[7],' | ',board[8],' | ',board[9])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[4],' | ',board[5],' | ',board[6])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[1],' | ',board[2],' | ',board[3])
    print('     |     | ')

def player_input():

    players_rand = 0 #random.randint(0,1)

    for number in turns:

            if number == 0:
                print('Welcome to Tic Tac Toe!')

            else:
                if players_rand == 0:
                    def Player_1_lucky():
                        print "===================="
                        print(number)
                        print "===================="
                        if number%2 != 0:
                            #def Player_1_turn():
                            answer1 = int(input('Player 1, assign the marker: X, to the board using a number from 1 to 9: '))
                            #answer_convert1 = int(answer1)

                            if answer1 == 1:
                                board[1] = ('X')
                                display_board(board)
                            #elif board[1] == ('X'):
                                #print('You cannot use this place because it is already taken. Please pick another number.')
                                #Player_1_turn()
                            if answer1 == 2:
                                board[2] = ('X')
                                display_board(board)
                            if answer1 == 3:
                                board[3] = ('X')
                                display_board(board)
                            if answer1 == 4:
                                board[4] = ('X')
                                display_board(board)
                            if answer1 == 5:
                                board[5] = ('X')
                                display_board(board)
                            if answer1 == 6:
                                board[6] = ('X')
                                display_board(board)
                            if answer1 == 7:
                                board[7] = ('X')
                                display_board(board)
                            if answer1 == 8:
                                board[8] = ('X')
                                display_board(board)
                            if answer1 == 9:
                                board[9] = ('X')
                                display_board(board)

                            #Player_1_turn()

                        elif number%2 == 0:
                            def Player_2_turn():
                                answer2 = int(input('Player 2, assign the marker: O, to the board using a number from 1 to 9: '))
                                #answer_convert2 = int(answer2)

                                if answer2 == 1 and board[1] == ('X'):
                                    #board[1] = ('X')
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 1:
                                    board[1] = ('O')
                                    display_board(board)
                                if answer2 == 2 and board[2] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 2:
                                    board[2] = ('O')
                                    display_board(board)
                                if answer2 == 3 and board[3] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 3:
                                    board[3] = ('O')
                                    display_board(board)
                                if answer2 == 4 and board[4] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 4:
                                    board[4] = ('O')
                                    display_board(board)
                                if answer2 == 5 and board[5] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 5:
                                    board[5] = ('O')
                                    display_board(board)
                                if answer2 == 6 and board[6] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 6:
                                    board[6] = ('O')
                                    display_board(board)
                                if answer2 == 7 and board[7] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 7:
                                    board[7] = ('O')
                                    display_board(board)
                                if answer2 == 8 and board[8] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 8:
                                    board[8] = ('O')
                                    display_board(board)
                                if answer2 == 9 and board[9] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 9:
                                    board[9] = ('O')
                                    display_board(board)

                            Player_2_turn()

                    Player_1_lucky()    

                if players_rand == 1:

                    if number%2 != board[0]:
                        answer2 = input('Play   er 2, assign the marker: O,  to the board using a number from 1 to 9: ')
                        answer_convert2 = int(answer2)
                    else:
                        answer1 = input('Player 1, assign the marker: X, to the board using a number from 1 to 9: ')
                        answer_convert1 = int(answer1)

display_board(board)
player_input()

答案 2 :(得分:0)

非常感谢你们的反馈意见。由于你的帮助,我认为当数字被字符串替换时,字符串字符串,for循环在游戏进行时不能正常运行,因为它依赖于数字来决定哪个玩家轮到它。我提出的解决方案涉及你们的解决方案。