-1

Taking a coursera course and stuck and defining functions. I'm not really sure what I'm doing wrong.

Here is the question being posed:

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 my sample code:

def print_seconds(hours, minutes, seconds):
    print("3600" + hours)
    print ("60" + minutes)
    print ("1" + seconds)

print_seconds(1,2,3)

These are what the current errors are:

Error on line 6:
    print_seconds(1,2,3)
Error on line 2:
    print("3600" + hours)
TypeError: must be str, not int
Red
  • 26,798
  • 7
  • 36
  • 58
IvanDraga
  • 9
  • 1
  • 1
    The reason for the specific error you're getting is that something like `"3600"` is a string, and the only thing you can do with `+` on a string is concatenate another string to it. But if you try to add a number and a string, that doesn't have a defined outcome, hence the error. – Random Davis Apr 29 '21 at 22:51
  • Does this answer your question? [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – Gino Mempin May 07 '21 at 04:44
  • There are many problems with this code, and no actual **question** is asked ("I'm not sure what I'm doing wrong." [does not qualify](https://meta.stackoverflow.com/questions/284236/)). – Karl Knechtel Sep 09 '22 at 07:29

6 Answers6

0

It's just:

def print_seconds(hours, minutes, seconds):
    total = (3600 * hours) + (60 * minutes) + seconds
    print(total)
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • Doesn't look like it likes that very much, here's the new error. "Error in grading script. See raw output log for details." – IvanDraga Apr 29 '21 at 22:51
-1

The issue is that you have quoted "3600" this makes it a string type variable, and python is complaining that it doesn't know how to add a string to an integer (which is what is being passed when you call print_seconds(1,2,3). The other answer is trying to tell you to not quote the integer values and the type casting/addition will work out for you.

-1

Or if you want you can just:

def print_seconds(hours, minutes, seconds):
    print((3600*hours) + (60*minutes) + seconds)
-1

If you're trying to get the total seconds, you have to multiple each parameter by an int, not a str. Also you're adding a string to the value, don't you want to multiply?

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

print_seconds(1,2,3)
Secret Agent
  • 399
  • 3
  • 10
-1
def print_seconds(hours, minutes, seconds):
    print (3600*hours + 60*minutes + seconds)
    

print_seconds(1,2,3)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
minhtetoo
  • 1
  • 1
-2

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

Print_seconds(1,2,3)

Gun6el
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 29 '22 at 22:47