Question

Python Programming: Why do I get UnboundLocalError? What does this mean and how can I fix...

Python Programming: Why do I get UnboundLocalError? What does this mean and how can I fix it?

Question: Write a function check_move(column, row) which returns a string describing a chess move to a given row and column on the chess board. Your program must check if the row and column entered are both valid. The column in a chess board is a letter ranging from A to H or a to h (inclusive) and the row is a number between 1 and 8 (inclusive).

  • column will be given to your function as a string and row will be given as an integer
  • If both coordinates are valid, such as 'E' and 2, your function must return 'The piece is moved to E2.'
  • Write a new version of check_move where, when there is an error in the chess move, the returned message specifies whether it is column or row value that is out of range:

  • If the column value is out of range, return 'The column value is not in the range a-h or A-H!'.
  • If the column value is in range but the the row value is out of range, return 'The row value is not in the range 1 to 8!'.
  • Your function should behave like this:

    >>> check_move('B', 4)
    'The piece is moved to B4.'
    >>> check_move('b', 4)
    'The piece is moved to B4.'
  • Your function should now work like this for invalid arguments:

    >>> check_move('I', 4)
    'The column value is not in the range a-h or A-H!'
    >>> check_move('F', 9)
    'The row value is not in the range 1 to 8!'
    >>> check_move('I', 9)
    'The column value is not in the range a-h or A-H!'

Here is my code, and how can I fix it?

def check_move(column,row):

if len(column) == 1 and (column in 'ABCEDFGH' or column in 'abcedfgh') and \
row in range(1, 9):
column = column.upper()
message = 'The piece is moved to ' + column + str(row) + '.'
else:
if (column not in 'ABCEDFGH' or column not in 'abcdefgh') and \
row in range(1, 9):
message = 'The column value is not in the range a-h or A-H!'
elif (column in 'ABCEDFGH' or column in 'abcedfgh') and \
row not in range(1, 9):
message = 'The row value is not in the range 1 to 8!'
return message

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#forget to mention one condition in the program is column and row both are not in the range we need to righh

first condition column in range row not in range

second condition column not in range row in range

third condition both are column and row not in range

then only didn't get unbounderror

#source code:

def check_move(column,row):
   if len(column) == 1 and (column in 'ABCEDFGH' or column in 'abcedfgh') and row in range(1, 9):
       column = column.upper()
       message = 'The piece is moved to ' + column + str(row) + '.'
   else:
       if (column not in 'ABCEDFGH' or column not in 'abcdefgh') and row in range(1, 9):
           message = 'The column value is not in the range a-h or A-H!'
       elif (column in 'ABCEDFGH' or column in 'abcedfgh') and row not in range(1, 9):
           message = 'The row value is not in the range 1 to 8!'
       elif (column not in 'ABCEDFGH' or column not in 'abcedfgh') and row not in range(1, 9):
           message = 'The column value is not in the range a-h or A-H! and The row value is not in the range 1 to 8!'
   return message
print(check_move('I', 9))

#if you have any doubt comment below.. if you like give thumbs up...

Add a comment
Know the answer?
Add Answer to:
Python Programming: Why do I get UnboundLocalError? What does this mean and how can I fix...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python programming Part VI: Move Starman by Movement Commands (20 points) Write a function move ()...

    Python programming Part VI: Move Starman by Movement Commands (20 points) Write a function move () that takes the following arguments, in this order I. board: A game board. 2. movement: A string that represents what kind of movement that Starman needs to perform. Four move ment commands are listed below: . 'U' : go up one row ·"D": go down one row : go left one column "R": go right one column Nole: You may assume that the movement...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. PROBLEM 1 INFORMATION IF YOU NEED IT AS WELL: Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem3. CODE PROVIDED FOR PROBLEM 2: import java.util.Scanner; public class Problem2 { public static void main( String [] args ) { //N...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { ...

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { }; struct game_board { }; void game_piece_init_default(struct game_piece* piece) { } void game_piece_init(struct game_piece* piece, char* new_label) { } char* game_piece_get_label(struct game_piece* piece) { return ""; } char* game_piece_to_string(struct game_piece* piece) { return ""; } void game_board_init(struct game_board* game_board, int rows, int cols) { } int game_board_is_space_valid(struct game_board* game_board, int row, int col) { return 0; } int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col) { return 0;...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int n;       ...

  • In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o i...

    In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...

  • # In this file, fill in the ... parts with lines of code. Do not # create new functions. from ran...

    # In this file, fill in the ... parts with lines of code. Do not # create new functions. from random import seed, randrange P=[" ♟♜♝♞♛♚"]; L,R,BL,TL=["▌▐▄▀"] BonR=WonR=WonB=DonR=DonB=RonB=GonR=GonB=RonG='\033[1;m\033[' WonR+='7;31;47m' # For drawing a white piece on a red background WonB+='7;30;47m' # For drawing a white piece on a black background DonR+='2;37;41m' # For drawing a dark piece on a red background DonB+='2;37;40m' # For drawing a dark piece on a black background GonR+='2;33;41m' # For drawing gold on a red...

  • I need just homework#6, I post homework# 5 to understand how to do it ? Please...

    I need just homework#6, I post homework# 5 to understand how to do it ? Please do it and please don't copy the answer CSCE 1030 Homework 5 Due: 11:59 PM on Monday, April 17,2017 PROGRAM DESCRIPTION: In this C++ program, you will start building the software infrastructure that will allow you to play a simplified version of the class game of Minesweeper that will display to the screen. In Homework 6, you will continue the work begun here to...

  • Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...

    Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLEASE PROVIDE COMMENTS AND OUTPUT! Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT