How to return a solved math problem within a defined return?

Advertisements

I am trying to get a (very) basic understanding of Python and its functions, and I found a way that it returns the text I want it to when used later within the script, but I cannot seem to have it output the solution to the math problem within the code and just the equation.

The code I am using is this:

def YearTime():
    return "Minutes in a year: (60*24)*365 "

YT = YearTime()

print(YT, "!", "Hello 2024!")

What I want it to output is

"Minutes in a year: 525600 ! Hello 2024!

but it just outputs

Minutes in a year: (60*24)*365  ! Hello 2024!

>Solution :

You must use an f string. Here is how you should do it.

def YearTime():
    return f"Minutes in a year: {(60*24)*365} "

YT = YearTime()

print(YT, "!", "Hello 2024!")

F string will evaluate the part that is wrapped in curly braces as standard python code. But do not include a lot of logic in it. But rather just simple evaluation.

Leave a ReplyCancel reply