将秒转换为小时,并在Python中获取输入和使用函数
代码:1-正在工作将秒转换为小时,并在Python中获取输入和使用函数,python,Python,代码:1-正在工作 def seconds_to_hours(seconds): hours = seconds / (60 * 60) return hours print(seconds_to_hours(5000)) seconds = input("Enter how many seconds convert to hours?: ") def seconds_to_hours(seconds): hours = seconds / (60
def seconds_to_hours(seconds):
hours = seconds / (60 * 60)
return hours
print(seconds_to_hours(5000))
seconds = input("Enter how many seconds convert to hours?: ")
def seconds_to_hours(seconds):
hours = seconds / (60 * 60)
return hours
print(seconds_to_hours)
代码:2-它不工作
def seconds_to_hours(seconds):
hours = seconds / (60 * 60)
return hours
print(seconds_to_hours(5000))
seconds = input("Enter how many seconds convert to hours?: ")
def seconds_to_hours(seconds):
hours = seconds / (60 * 60)
return hours
print(seconds_to_hours)
错误是:
如何解决此问题?不要对获取输入的主参数
seconds
使用两次相同的名称,也不要将输入转换为int
def seconds_to_hours(secs):
return secs / (60 * 60)
seconds = int(input("Enter how many seconds convert to hours?: "))
print(seconds_to_hours(seconds))
谢谢您的快速回复。使用
秒作为参数有什么问题?它将屏蔽全局变量,这是一件好事。OP代码的主要问题是函数从未真正被调用,而不是变量名。不过对int
的调用很好。@ggorlen无论您喜欢什么名称secs
,但不相同,以避免误会这是更好的选择。我必须在输入中写入数据类型。