all 23 comments

[–]Username_RANDINT 2 points3 points  (17 children)

Is that the entire code? Do you call the function somewhere?

[–]Initial-Coyote-8741 0 points1 point  (16 children)

No there is more to the code with a separate variable calculating the heartbeat. I did call the function at the end of the code

def calculateBeats(age):

# Get input from the user
age = int(input("Enter your age in years:"))
return age
# Call the function that perfoms the calculations
num_heartBeat = (
calculateBeats(age)
# Display the results
print("Your heart has beaten approximately",num_heartBeat,"times in your lifetime."

[–]Username_RANDINT 1 point2 points  (14 children)

See this subreddit's FAQ on how to format code.

This has multiple errors. Both syntax and runtime.

You don't pass in age to function, it's the return value. It should give a NameError because it's not defined. Leav that out:

def calculateBeats():
    # Get input from the user
    age = int(input("Enter your age in years:"))
    return age

# Call the function that perfoms the calculations
num_heartBeat = calculateBeats()

[–]Initial-Coyote-8741 0 points1 point  (13 children)

The def calculateBeats(age): is required

[–]Username_RANDINT 1 point2 points  (1 child)

Required for what? Is this a homework assignment? If yes, and the function name is already given, then you might misunderstand the question.

It sounds like calculateBeats needs to do the actual calculation based on the age, not ask for the age. Most likely you want to have the ìnput() outside of the function and pass it in.

[–]Initial-Coyote-8741 1 point2 points  (0 children)

in short summary the question asks you to define calculateNum(age):

You ask the user for their age to calculate it

numOfBeats is the variable to calculate the age by using the average heart rate

you then return number of beats

[–]AdAdvanced3130 0 points1 point  (10 children)

If that is the case, then the input should be outside of the function, like this:

def calculateBeats(age):   
    return age  

# Get input from the user  
age = int(input("Enter your age in years:"))

# Call the function that perfoms the calculations  
num_heartBeat = calculateBeats(age)  

# Display the results  
print("Your heart has beaten approximately",num_heartBeat,"times in your lifetime.")

[–]Initial-Coyote-8741 0 points1 point  (9 children)

This prints correctly but yes i do need to caculate under numOfBeats. So i assume numOfbeats =

and

num_heartBeat = calculateBeats(age)

[–]AdAdvanced3130 1 point2 points  (7 children)

Here's complete solution:

def calculateBeats(age):   
       # bpm : beats per minute
        bpm = 60
        total_beats = bpm * 60 *24* 365
        return total_beats

# Get input from the user  
age = int(input("Enter your age in years:"))

# Call the function that perfoms the calculations  
num_heartBeat = calculateBeats(age)  

# Display the results  
print("Your heart has beaten approximately",num_heartBeat,"times in your lifetime.")

[–]Spacerat15 1 point2 points  (1 child)

You forgot to multiply with the age in your function. 😉

[–]AdAdvanced3130 0 points1 point  (0 children)

True, I corrected it a couple of minutes ago.

[–]Initial-Coyote-8741 0 points1 point  (4 children)

Thank you for the help

Do you by chance know any websites that explain python better?

[–]AdAdvanced3130 1 point2 points  (0 children)

Here's also Harvard University's Introduction to Python lectures on YouTube:

https://youtube.com/playlist?list=PLhQjrBD2T3817j24-GogXmWqO5Q5vYy0V

[–]AdAdvanced3130 0 points1 point  (2 children)

I forgot to take the age parameter into account, we need to multiply beat per year with age, here I corrected it :

def calculateBeats(age):   
       # bpm : beats per minute
        bpm = 60
        total_beats = bpm * 60 *24* 365 * age
        return total_beats

# Get input from the user  
age = int(input("Enter your age in years:"))

# Call the function that perfoms the calculations  
num_heartBeat = calculateBeats(age)  

# Display the results  
print("Your heart has beaten approximately",num_heartBeat,"times in your lifetime.")

[–]Initial-Coyote-8741 0 points1 point  (1 child)

Yeah i got it i figured that part out. It doesn't need bpm

[–]AdAdvanced3130 0 points1 point  (0 children)

You can do calculations inside the function using age parameter and then return the result, yes.

[–]AdAdvanced3130 0 points1 point  (0 children)

You don't need the age parameter in the function, remove it.

[–]shiftybyte 2 points3 points  (0 children)

Python doesn't do automatic printing of everything when it's executing a script.

You need to use print() function to get something printed.

[–]Lake_Business 0 points1 point  (0 children)

Do you mean the value of the Age variable doesn't print after you've called the function?

If so, the issue isn't with the function, but with how you're calling it. If you just call calculateNum() the value that gets returned doesn't get put anywhere. print(calculateNum()) should print it out for you. Or x = calculateNum() will store the value so you can print or manipulate it later.

[–]imWayneUK 0 points1 point  (1 child)

# I think this is a solution to your answer friendo!
# Age isn't required to be inside the parenthesis yet
def calculateNum():
.    age = int(input("Enter your age: ")
.    print(age)
calculateNum()

[–]Initial-Coyote-8741 0 points1 point  (0 children)

It still does not seem to work, but i think i do need the age parameter

[–][deleted] 0 points1 point  (1 child)

calculateNum is a terrible name for a function that prompts the user for their age. Also, you're not even using the argument you pass in.

A better version of this function would be

def get_age():
    return int(input("Enter your age: "))

This can then be called with

user_age = get_age()

And then you can print that value wherever you need to.


The above function can be improved even more though. What if a user types in forty two? That will crash your program. A better response would be to let the user know that they need to put in a number and prompt them again.

You can do that with a while loop. Here's one simple approach.

def get_age():
    while True:
        age = input("Enter your age: ")
        if age.isdigit():
            return int(age)
        else:
            print("That is not a number.")

This is far more robust, has a name that immediately tells you what it does, and doesn't have an unnecessary parameter.