TransWikia.com

Why am I being asked to defined a variable that already been defined?

Stack Overflow Asked by user716255 on October 1, 2020

I am trying to solve homework problem.

Flesh out the body of the print_seconds function so that it prints the total amount of seconds given the hours, minutes, and seconds function parameters. Remember that there are 3600 seconds in an hour and 60 seconds in a minute.

Here is what I have tried.

def print_seconds(hours, minutes, seconds):
    total_seconds = hours * 3600 + minutes * 60 + seconds
print(total_seconds)
print_seconds(1,2,3)

I am getting this error message.

enter image description here

I dont understand why I am getting this error. I defined the variable inside the function.

2 Answers

You have to indent the 2nd to last line print(total_seconds) like this, at the moment, it's out of scope.

def print_seconds(hours, minutes, seconds):
   total_seconds = hours * 3600 + minutes * 60 + seconds
   print(total_seconds)

print_seconds(1,2,3)

Output:

3723

Correct answer by FishingCode on October 1, 2020

The reason you are getting an error is because of a scope issue. You need to put the print(total_seconds) code in the print_seconds() function, like this:

def print_seconds(hours, minutes, seconds):
    total_seconds = hours * 3600 + minutes * 60 + seconds
    print(total_seconds)

print_seconds(1,2,3)

Answered by Darian Benam on October 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP