Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading