-1

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!
4
  • 4
    use docs.python.org/3/tutorial/… Commented Jun 3, 2024 at 0:22
  • There are numerous ways to do this, including a simple f-string, but you do need to learn the difference between string expressions and arithmetic expressions and I fear an f-string will just blur the distinctions in your mind. Commented Jun 3, 2024 at 0:22
  • 1
    I think, it's simpler to think of it as 2 separate steps. STEP 1, compute the value of (60*24)*365 and store that in a variable, say result. STEP 2, figure out how to format a string with the result variable value. STEP 2 would lead you to the How do I put a variable’s value inside a string? These 2 steps are not just for solving math problems, but applies as well to any kind of values you want to put into a string. Commented Jun 3, 2024 at 0:36
  • 1
    Helpful or related Formatted string with calculated values in Python Commented Jun 3, 2024 at 0:36

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.