Python Forum
Really stumped now - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Really stumped now (/thread-29687.html)



Really stumped now - Milfredo - Sep-16-2020

I thought I had fixed my problem. In fact I marked post solved. The following code works as long as user selects Race 1. But select any other race number and it won't print out. Throws the following error.

def load_track_to_Handi():
	my_tracklabel= Label(root, text ="                                                                               ", fg= "#F50808", font = ("sans_serif" , 16)).place(x=380, y=430)
	read_csvfile_into_array()
	
		
	global track_abrev
	global race_number
	global xx
	
	#if fileFound ==True:

		
		 
	
	
	hcount = 0
	
	with open( 'C://guis/f_racefiles/'+ track_abrev + '.csv' , 'r') as race_file:

		
		df = pd.read_csv(race_file, header =None)
		
		xx = df.iloc[ :,0:19 ].values
		
		

		for i in range(0,len(xx)):
			if int(xx[hcount][1])== int(race_number):
			 
				print(xx[hcount][18])					
				hcount+=1	
Select race 1 and this is the print out:
TICKET TO RICHES
FLASHY PATCH
HAUTE PROSPECT
MISS VICKI
HONEST GINGER
ABOGADA
But if any other race number selected it throws this error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "testloop.py", line 134, in select_track
load_track_to_Handi()
File "testloop.py", line 95, in load_track_to_Handi
if int(xx[hcount][0])== int(race_number):
ValueError: invalid literal for int() with base 10: 'AP '


I don't understand this at all. I thought the [1] in the code stood for column 1.


RE: Really stumped now - bowlofred - Sep-16-2020

It looks like race_number is set to "AP". When passed to int(), python can't convert it to a number, so the error is thrown.


RE: Really stumped now - Milfredo - Sep-16-2020

When I click the button that represents race 1 it works fine. Click any other button, say race 2 or race 3 or race 4 and the error gets thrown. I don't understand why.


RE: Really stumped now - bowlofred - Sep-16-2020

That depends on how race_number is set. But it appears to be set in some other code.


RE: Really stumped now - Milfredo - Sep-17-2020

It is. But I make sure both are set to integers in my code in the conditional statement. I can not find anywhere that addresses this issue. That's why I need some help. I have looked everywhere and watched a bunch of videos. If race 1 works, then the other race numbers should work since they are all coded the same.


RE: Really stumped now - bowlofred - Sep-17-2020

Put in some debugging statements. Everywhere race_number is assigned, put in a logging statement that says "I'm setting race_number to {race_number}". Or some that check periodically what it's set to.

Then narrow down to the section that is setting it.


RE: Really stumped now - buran - Sep-17-2020

(Sep-17-2020, 12:33 AM)Milfredo Wrote: It is. But I make sure both are set to integers in my code in the conditional statement. I can not find anywhere that addresses this issue. That's why I need some help. I have looked everywhere and watched a bunch of videos. If race 1 works, then the other race numbers should work since they are all coded the same.
you cannot argue with the interpreter. provide a sample csv file, so that we are able to reproduce the error. edit your code to be minimal reproducible example (i.e. no GUI, no user input, etc.)


RE: Really stumped now - Milfredo - Sep-17-2020

Thank you guys for the help. I figured out the solution. I changed the value of race number to an integer at time button clicked and passed it as an Integer. Converted the file number from string value to integer in the conditional if statement. Thanks again for all the help.