Question:
Help with python programming?
ziggy
2010-10-04 23:21:40 UTC
This is part of my code, but I keep getting a syntax error. SyntaxError: invalid syntax
eecs python hw4.py
File "hw4.py", line 34
hours2 = (int(y[0]) * 60**2)
^
SyntaxError: invalid syntax can anyone help me figure out why? I am trying to convert the time "H:MM:SS" into seconds so I can tell which time will be bigger


def maxtimes(time1, time2) :
x = gettime(time1)
y = gettime(time2)
hours1 = (int(x[0]) * 60**2)
minutes1 = (int(x[1]) * 60)
seconds1 = (int(x[2])
hours2 = (int(y[0]) * 60**2)
minutes2 = (int(y[1]) * 60)
seconds2 = (int(y[2])
totalseconds1 = hours1 + minutes1 + seconds1
totalseconds2 = hours2 + minutes2 + seconds2
if totalseconds1 > totalseconds2:
return (time1)
if totalseconds2 > totalseconds1:
return (time2)
Four answers:
Dave
2010-10-05 23:11:49 UTC
hours2 = (int(y[0]) * 60**2)

^

SyntaxError: invalid syntax



seconds1 = (int(x[2])

hours2 = (int(y[0]) * 60**2)



The line above that error line is missing a closing parenthesis.



Often a syntax error at the beginning of a line will be due to missing/extra parentheses in the line before.
2010-10-04 23:57:35 UTC
The code looks fine, you'll have to post the whole code and full error. I take it line 34 is hours2 = (int(y[0]) * 60 ** 2) ?



ADDED:



Hard to read whats wrong from this... what do you enter as time2? Run the debugger and see whats hiding in y and check what it takes for y[0] - it might not be a string that can be converted to integer, therefore causing a syntax error! - for example y[0] = "12:"

(y[0])*60**2 is then "12:12:12:12:12:12: etc" and int("12:") is a syntax error.
Graham B
2010-10-06 01:13:19 UTC
Hi

The previous line is missing a closing bracket:





seconds1 = (int(x[2]) <---------------- Needs a ) here!

hours2 = (int(y[0]) * 60**2)



Regards

G
grigsby
2016-11-03 08:09:15 UTC
in case you have examine something approximately sound in "Console-Mode" Python, this is by using the fact there's no sound characteristic with Console Python. you ought to learn wxPython or PyGame or perhaps even Tkinter (i think of helps sound useful properties)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...