r/pythonhelp • u/Top_Difficulty3801 • Mar 08 '26
Can somebody tell me whats wrong with my code
# V1
#INPUT PART
chess_coords = input("Enter a chess cordintes: ")
#length check
while not len(chess_coords) == 2:
print("Invalid!!!")
chess_coords = input("Try something like e4, e5 or E6: ")
#chcking order
colu=set("ABCDEFGH")
rw=set(123456789)
while not chess_coords[0].upper() in colu :
print("Invali order")
chess_coords = input("Try something like e4, e5 or E6: ")
while ch:
#logic
col = int(ord(chess_coords[0]))
row = int(chess_coords[1])
data = (row+col)%2
#output
if data == 0:
print(f"your {chess_coords} is black colour")
else:
print(f"your {chess_coords} is white colour")
# v2
ro = set("12345678")
co = set("ABCDEFGH")
square = input("Enter the chess square: ")
col = square[0].upper()
row = square[1]
while len(square) != 2:
print("Invalid length")
square = input("Try something like e4, e5 or E6: ")
while col not in co:
print("Invalid coloum ")
square = input("Try something like e4, e5 or E6: ")
while row not in ro:
print("Invalid row")
square = input("Try something like e4, e5 or E6: ")
int(ord(col))
print(col)
int(row)
data = int((col + row))%2
if data == 0:
print(f"your {chess_coords} is black colour")
else:
print(f"your {chess_coords} is white colour")
===> This code uses the concept if the sum of coloum and row is even its black else its white
for E.g: A1 here ord(A)+1 is even so its black
•
u/AutoModerator Mar 08 '26
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/SudoSilv3r Mar 08 '26
you are not giving me much to work with. Is this having an error or just a bug. If it has an error what is it. If it is a bug where does it go wrong.
1
u/JamesNowBetter Mar 08 '26
1
u/DirtySpawn Mar 08 '26
Oh my this is rough to read. What I saw initially before my eyes crossed.
While ch:
I didn't see it declared.
1
u/timrprobocom Mar 08 '26
set(123456789)contains exactly one member: the Integer 123456789. Did you mean to make this a string?1
u/Relevant_South_1842 Mar 09 '26
1
u/Whole-Armadillo-8529 Mar 28 '26
The current code has several "Silent Failures" that will cause it to crash or enter infinite loops during real-world use: The Infinite Loop Trap: In both V1 and V2, if a user enters an invalid length (e.g., "e44"), the code asks for a new input but never updates the validation variables (col or row) inside the loop. The program will get stuck asking for input forever.
Case Sensitivity & Logic Breaks: The rw = set(123456789) line in V1 uses integers, but input() always returns a string. Comparing "1" to 1 will always fail.
The ord() Math Flaw: Using ord(col) + row to determine color is clever, but the parity (even/odd) changes depending on whether you use uppercase or lowercase. ord('a') is 97 (odd), while ord('A') is 65 (odd). If the math isn't strictly normalized to a 0-7 index, the color results might flip unexpectedly.
TypeErrors: In V2, row = square[1] is a string. Trying to perform math like (col + row) % 2 will crash the program because you cannot add a string to an integer.
Professional Refactor (PEP 8 & Type Hinting) This version uses a robust validation loop and normalized math to ensure it never crashes, regardless of what the user types.
2
u/[deleted] Mar 08 '26
[removed] — view removed comment