By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Help with a heartbeat program

P: 64
Hi. I have created a program which works correctly, and was just wondering if someone could help me in writing another program which works in the same way, but if the code is different. Below is what the program has to do:

If a human heartbeats on average once a second for 78 years, how many times does the heart beat in a lifetime? (use 365.25 days per year). Re-write your program to prompt the user to input an average rate and total number of years and output the resulting heart beats in a lifetime.
This is the program I have created:

Expand|Select|Wrap|Line Numbers
  1. print "--------Welcome To The Heartbeat Program--------"
  3. averagerate = input ("\nPlease enter your average heart rate:")
  5. totalyears = input ("Please enter the total number of years:")
  7. heartrate = 60 * 60 * 24 * 365.25 
  9. result = averagerate * heartrate * totalyears
  11. print "\nThe total heartbeats is:", "%.7d" % (result)
  13. print "\n--------End Program--------"
Can someone help me then? Many thanks.
Dec 2 '08 #1
Share this Question
Share on Google+
14 Replies

Expert Mod 2.5K+
P: 2,851
I think an accepted heart rate is beats per minute (bpm). A general formula could therefore be written as a function as follows:
Expand|Select|Wrap|Line Numbers
  1. def heartbeats(rate, years):
  2.     return int(rate*60*24*365.25*years)
Now it's a matter of getting the input from the user and formatting the output.
Dec 2 '08 #2

P: 64
the heart beat code works fine, but i need another soloution to the code which, does same thing, but has to be written diffrently to the one i have done

many thanks
Dec 2 '08 #3

Expert Mod 2.5K+
P: 2,851
Your code is almost there! Just substitute this line at the appropriate location:
Expand|Select|Wrap|Line Numbers
  1. result = heartbeats(averagerate, totalyears)
Dec 2 '08 #4

Curtis Rutland
Expert 2.5K+
P: 3,256
@imran akhtar
I must wonder why you would need to re-write some code in a different way...the only thing I can think of is that you're a student either helping someone, or having them help you...

If I am wrong in this assumption, I apologize in advance, but we don't help with homework, especially not re-writing homework so that it looks different.

Dec 2 '08 #5

P: 64
no i need another code, due to the fact i have to make two diffrent codes which does the same thing, i have one code which already works, but need another one, which i am struggling to come up with.

if any one can come up with another code, which does the same thing, that will be great, or give any hints to write seconde the code.

many thanks
Dec 3 '08 #6

Curtis Rutland
Expert 2.5K+
P: 3,256
You have just repeated your still haven't explained why you need two different sets of code to do the exact same thing.

Please explain.
Dec 3 '08 #7

P: 64
Nevermind, I have come up with a solution. No, I am not a student. Bye.
Dec 3 '08 #8

P: 64
made the chagnes wht you have suggested , but getting problems such as: does not sems to totla up.

below is the error message :
(print "\nyour total heartbeats is:", "%.7d" (result)
TypeError: 'str' object is not callable
Attached Files
File Type: txt heartbeat.txt (732 Bytes, 362 views)
Dec 17 '08 #9

Expert 100+
P: 849
When using string formatting, the syntax is
Expand|Select|Wrap|Line Numbers
  1.  "string with % signs" % (values to replace the format specifiers)
You're missing the % in between.
Dec 17 '08 #10

Expert Mod 2.5K+
P: 2,851
You are missing the modulo operator in the print format code:
Expand|Select|Wrap|Line Numbers
  1. print "\nyour total heartbeats is:", "%.7d" % (result)
You have other problems. Your code:
Expand|Select|Wrap|Line Numbers
  1. print "--Welcome To The Heartbeat Program---"
  3. def heartbeats(rate, years): 
  4.     return int(rate*60*24*365.25*years)
  6. rate = input ("\nWhat isaverage heart rate:")
  8. years = input ("Enter total number of years:")
  10. heartbeat = 60 * 60 * 24 * 365.25
  12. result = rate * rate * years
  14. #result = averagerate * heartrate * totalyears
  16. print "\nyour total heartbeats is:", "%.7d" (result)
  18. print "\n----End Program----"
You should clarify what the heart rate is. Let us assume the rate should be in units of beats per minute. There are 60 minutes in an hour, 24 hours in a day, and 365.25 days in a year. The calculation for the total number of beats is:
Expand|Select|Wrap|Line Numbers
  1. number_beats = rate*60*24*365.25
See how the units cancel out and you end up with only beats:
Expand|Select|Wrap|Line Numbers
  1. '''
  2. beats     minute     hour     day
  3. ------ * -------- * ------ * ------ * year = beats
  4. minute    hour       day      year
  5. '''
You define function heartbeats(), but you do not use it. HTH
Dec 17 '08 #11

P: 64
yeh thanks it now runs, but how do i know it works, baically i have sent attched txt file, so you can double cheack if it fucntion correctly with correct figures.

once agian thanks for the help
Attached Files
File Type: txt heartbeat.txt (706 Bytes, 377 views)
Dec 17 '08 #12

Expert Mod 2.5K+
P: 2,851
You have an error:
Expand|Select|Wrap|Line Numbers
  1. result = rate * rate * years
If you are not going to use function heartbeats(), why don't you delete it?

Dec 17 '08 #13

P: 64
no it now runs, and outputs answer, but i want to know if you can double cheack, and see if it is the correct answer.
Dec 17 '08 #14

Expert Mod 2.5K+
P: 2,851
For a heart rate of 60 beats per minute, I calculate 31557600 heart beats in one year.

Dec 17 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.