Python Forum
Wrong output, something is missing in my codes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrong output, something is missing in my codes
#1
I'm a freshman, and have this assignment to just fill inn the missing codes/def functions.
I think I'm done but get the wrong output.

When I run it, the program ask for an input, example 0,1. This input will split and represent a place in the board.
But I got the output "Wrong entry. Think again!". The place is not taken yet.
Can someone help me, or tell me what's missing/wrong with my functions?

I use Python 3.8.5

def print_board(board):
    print(board[0][0] + '|' + board[0][1] + '|'  + board[0][2])
    print("-+-+-")
    print(board[1][0] + '|' + board[1][2] + '|' + board[1][2])
    print("-+-+-")
    print(board[2][0] + '|' + board[2][1] + '|' + board[2][2])



def check_winner(board, current):
    if board[0][0] == board[0][1] == board[0][2] == current:
        return True
    elif board[1][0] == board[1][1] == board[1][2] == current:
        return True
    elif board[2][0] == board[2][1] == board[2][2] == current:
        return True
    elif board[0][0] == board[1][0] == board[2][0] == current:
        return True
    elif board[0][1] == board[1][1] == board[2][1] == current:
        return True
    elif board[0][2] == board[1][2] == board[2][2] == current:
        return True
    elif board[0][0] == board[1][1] == board[2][2] == current:
        return True
    elif board[2][0] == board[1][1] == board[0][2] == current:
        return True
    else:
        return False



  

def update_board(board, row, col, current_player):
    board[row][col] = current_player

    


def verify_entry(board, row, col):
    if board[row][col] == '':
        return True
    else:
        return False
    
    if row in range(0,3) and col in range(0,3):
        return True
    else:
        return False


board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
print_board(board) #prints an empty board
current = None
moves = 0
while (moves < 9 and not check_winner(board, current)): #game over?
    #play
    current = "X" if (current == None or current == "O") else "O"
    row, col = map(int, input("Enter the move for " + current +": ").split(","))
    while not verify_entry(board, row, col):
        print("Wrong entry. Think again!")
        row, col = map(int, input("Enter the move for " + current +": ").split(","))
    update_board(board, row, col, current)
    print_board(board) #prints the current board
    moves = moves + 1 
Reply
#2
Your if statement in line 41 is looking for an empty string (two single quotes with nothing between them). Is that what you are populating the board with on line 52?

Also, be aware that your lines 46 through 50 will never run. Lines 41 through 44 are going to return either True or False, at which point the function stops executing.
Reply
#3
(Sep-28-2020, 11:12 AM)GOTO10 Wrote: Your if statement in line 41 is looking for an empty string (two single quotes with nothing between them). Is that what you are populating the board with on line 52?

Also, be aware that your lines 46 through 50 will never run. Lines 41 through 44 are going to return either True or False, at which point the function stops executing.

Line 52 to 65 are predefined in the assignment. I'm not sure but line 41 are supposed to verify entry. Thanks for feedback :) Can I just drop line 46 to 50?
Reply
#4
Line 52 is populating the board with whitespace in the form of a space between two single quotes. Line 41 is checking for an empty string, which is not the same thing as the whitespace added in line 52. So, with the code as currently written, the verify_entry() function is going to return False every time since there are no empty strings in board when it is created on line 52.

Dropping lines 46 through 50 won't change anything about the way your code currently works. However, it's probably a good idea to confirm that the row and column entered by the user are in a valid range. You need to change your approach to doing this, since a function can only return once and does not continue to execute after return.

One way to handle this would be to code your function so that it returns False if any required condition is unsatisfied, otherwise it returns True after all the conditions have been evaluated.

Pseudocode example:
def validate(something):
    if not (<condition 1> and <condition 2> and <condition 3>):
        return False  # returns False if the conditions above are not all met
    return True  # this line only executes if we haven't already returned False, which means all conditions were met so we return True
You can make it even simpler and skip the if, since return can return the evaluation of a boolean expression:
def number_equals_three(num):
   return num == 3  # returns the evaluation of "num == 3" as either True or False
Reply
#5
Thank you so much, I will work on that :)
Reply
#6
To address some other aspects of the code: there are too many repetitions and therefore code is not DRY (don't repeat yourself)

Instead of entering indices manually one can use tools built-in into Python: 'iterate over rows and separate centered values on rows with '|' and separate rows with separator string':

def game_state(board):
    separator = f'\n{"+".join("-" * 3 for i in range(3))}\n'
    print(*('|'.join(state.center(3) for state in row) for row in board), sep=separator)
Same applies to check_winner. Also it is worth of mentioning that there is point of checking winner only starting from fifth move.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong? Or is the question setup wrong? musicjoeyoung 3 3,455 May-18-2020, 03:38 PM
Last Post: musicjoeyoung
  Wrong output on my code. JTNA 2 9,166 Apr-04-2019, 01:55 PM
Last Post: JTNA

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020