AnswerBun.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!

Related Questions

What is the purpose of session.endConversation()

1  Asked on January 23, 2021 by flea

 

To convert not null datetime into null. c#

1  Asked on January 23, 2021 by mashail-furqan

     

Updating Values in ArrayList in Java

2  Asked on January 23, 2021 by billy-dan

     

C++: Can a Template be friend of a Class?

1  Asked on January 23, 2021 by ali-rojas

     

Retrieving image from database and showing it Laravel

5  Asked on January 22, 2021 by parsa_237

   

f-strings works on python IDLE but not on Sublime Text

3  Asked on January 22, 2021 by david-jiang

     

SQL Select List mulitple values with case then

2  Asked on January 22, 2021 by user14082433

     

setState does not update TextFormField when use initialValue

1  Asked on January 22, 2021 by wondertalik

 

IndexError: Index out of range Python Error

1  Asked on January 22, 2021 by user13669275

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP