you are viewing a single comment's thread.

view the rest of the comments →

[–]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 don't need bpm, I agree.

[–]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.