Help with a heartbeat program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    Help with a heartbeat program

    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:

    Code:
    print "--------Welcome To The Heartbeat Program--------"
    
    averagerate = input ("\nPlease enter your average heart rate:")
    
    totalyears = input ("Please enter the total number of years:")
    
    heartrate = 60 * 60 * 24 * 365.25 
    
    result = averagerate * heartrate * totalyears
    
    print "\nThe total heartbeats is:", "%.7d" % (result)
    
    print "\n--------End Program--------"
    Can someone help me then? Many thanks.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I think an accepted heart rate is beats per minute (bpm). A general formula could therefore be written as a function as follows:
    Code:
    def heartbeats(rate, years):
        return int(rate*60*24*365.25*years)
    Now it's a matter of getting the input from the user and formatting the output.

    Comment

    • imran akhtar
      New Member
      • Nov 2008
      • 64

      #3
      heartbeat

      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

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Your code is almost there! Just substitute this line at the appropriate location:
        Code:
        result = heartbeats(averagerate, totalyears)

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Originally posted by imran akhtar
          ... but i need another soloution to the code which, does same thing, but has to be written diffrently to the one i have done...
          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.

          MODERATOR

          Comment

          • imran akhtar
            New Member
            • Nov 2008
            • 64

            #6
            heart beatcode

            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

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              You have just repeated your problem....you still haven't explained why you need two different sets of code to do the exact same thing.

              Please explain.

              Comment

              • imran akhtar
                New Member
                • Nov 2008
                • 64

                #8
                Nevermind, I have come up with a solution. No, I am not a student. Bye.

                Comment

                • imran akhtar
                  New Member
                  • Nov 2008
                  • 64

                  #9
                  heartrate

                  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

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    When using string formatting, the syntax is
                    Code:
                     "string with % signs" % (values to replace the format specifiers)
                    You're missing the % in between.

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      You are missing the modulo operator in the print format code:
                      Code:
                      print "\nyour total heartbeats is:", "%.7d" % (result)
                      You have other problems. Your code:
                      Code:
                      print "--Welcome To The Heartbeat Program---"
                      
                      def heartbeats(rate, years): 
                          return int(rate*60*24*365.25*years)
                      
                      rate = input ("\nWhat isaverage heart rate:")
                      
                      years = input ("Enter total number of years:")
                      
                      heartbeat = 60 * 60 * 24 * 365.25
                      
                      result = rate * rate * years
                      
                      #result = averagerate * heartrate * totalyears
                      
                      print "\nyour total heartbeats is:", "%.7d" (result)
                      
                      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:
                      Code:
                      number_beats = rate*60*24*365.25
                      See how the units cancel out and you end up with only beats:
                      Code:
                      '''
                      beats     minute     hour     day
                      ------ * -------- * ------ * ------ * year = beats
                      minute    hour       day      year
                      '''
                      You define function heartbeats(), but you do not use it. HTH

                      Comment

                      • imran akhtar
                        New Member
                        • Nov 2008
                        • 64

                        #12
                        heart beat

                        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

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          You have an error:
                          Code:
                          result = rate * rate * years
                          If you are not going to use function heartbeats(), why don't you delete it?

                          -BV

                          Comment

                          • imran akhtar
                            New Member
                            • Nov 2008
                            • 64

                            #14
                            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.

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              For a heart rate of 60 beats per minute, I calculate 31557600 heart beats in one year.

                              -BV

                              Comment

                              Working...