Python:持久性未识别语法错误

问题描述 投票:0回答:1

我有一段简单的代码:

total_time = float(input("Enter total time in seconds: "))
eq_days =  total_time // 86400
eq_hrs = (total_time % 86400) // 3600
eq_mins = ((total_time % 86400) % 3600) // 60
eq_secs = (((total_time % 86400) % 3600) % 60
print('Equivalent to {} days, {} hours, {} minutes and {} seconds.'.format(eq_days, eq_hrs, eq_mins, eq_secs))

最后一行(打印)一直返回无效的语法错误,并且似乎与语法没有真正的关系,因为如果我将其更改为print(“ OK”),它仍然会给出相同的错误。

我什至尝试删除该行-它将在解析时给出“意外的EOF”。

任何想法为何会发生以及如何纠正?

python python-3.x syntax
1个回答
0
投票

[print()语句之前的行中有一个括号。

eq_secs = (((total_time % 86400) % 3600) % 60 # no closing bracket

上面的代码行由于额外的方括号引起错误。下面的代码已得到纠正,可以正常工作-

total_time = float(input("Enter total time in seconds: "))
eq_days =  total_time // 86400
eq_hrs = (total_time % 86400) // 3600
eq_mins = ((total_time % 86400) % 3600) // 60
eq_secs = ((total_time % 86400) % 3600) % 60
print('Equivalent to {} days, {} hours, {} minutes and {} seconds.'.format(eq_days, eq_hrs, eq_mins, eq_secs))
© www.soinside.com 2019 - 2024. All rights reserved.