Python Forum
Issues with functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with functions
#1
Need help with my homework. It's apparent to me that I don't fully understand how to pass information to a function. I'm following examples the professor has provided and I've googled a bunch of stuff about functions. I just don't know why it's not clicking. Can someone please explain this to me like I'm a 2-year-old? Wall

Here's what I need to do:
Quote:When getting into a fitness program, you may want to figure out your target heart rate so you don’t overexert yourself. The Karvonen heart rate formula is one method you can use to determine your rate. Create a program that prompts for your age and your resting heart rate. Use the Karvonen formula to determine the target heart rate based on a range of intensities from 55% to 95%. Generate a table with the results as shown in the example output (format matters). The formula is

TargetHR = (((220 - age) - restingHR) * intensity) + restingHR

Here's what it should look like (THIS DOESN'T DISPLAY CORRECTLY - PLEASE SEE ATTACHMENT AT THE BOTTOM OF THIS POST):
Quote:Resting Pulse: 65 Age: 22

Intensity | Rate
----------|--------
55% | 138 bpm
60% | 145 bpm
65% | 151 bpm
: : (extra lines omitted)
85% | 178 bpm
90% | 185 bpm
95% | 191 bpm

Here's my code:

def problem6_1():
    
    table_width = 10
    table_height = .95
    def user_input():
        age = int(input("What is your age?"))
        restingHR = int(input("What is your resting pulse?"))
        return(age,restingHR)
 
    
    def print_header(table_width):
        print("Intensity | Rate".format(''), end="")
       #print("{:10}|".format('-'))
        
    def print_table(table_width, table_height, age, restingHR):
        for i in range(.55, table_height+.5):
            print("{:10}|".format(i), end="")
            for j in range(1, table_width+1):
                print("{:10}".format((((220 - age) - restingHR) * i) + restingHR), end="")
            print()
     
    user_input()
    print_header(table_width)
    print_table(table_width, table_height)
            
problem6_1()
    
Here's my error:
Error:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-49e95b7c904f> in <module>() 24 print_table(table_width, table_height, age, restingHR) 25 ---> 26 problem6_1() 27 28 <ipython-input-2-49e95b7c904f> in problem6_1() 22 user_input() 23 print_header(table_width) ---> 24 print_table(table_width, table_height, age, restingHR) 25 26 problem6_1() NameError: name 'age' is not defined
What am I doing wrong? Additionally, how do I add the underscores beneath Intensity and Rate? They're not the same length and he says formatting counts.
Quote:Intensity | Rate
----------|--------
   
Reply
#2
def print_table(table_width, table_height, age, restingHR):
when you create a function, the attributes are required to be passed, unless you give then a default value.
to do that, the definition of the function would be something like:
def print_table(table_width, table_height, age=20, restingHR=1):
the values I used are not designed to work with you code, but only as an example.
When you include defaults, once an attribute has a default value, the remaining attributes that are declared after
the one which was given a default, must also have default values, example:
def print_table(table_width, table_height, age=20, restingHR):
would be invalid, but:
def print_table(table_width, table_height, age, restingHR=1):
would be valid.
any attributes that do not have default values are required to be passed when calling the function, the others are optional.
Reply
#3
You've only defined age in the function user_input. It is private to that function and not available outside of that function. You either need to pass the parameters you want to share in the first place as arguments or return the information you require from the function (with due regard for the data structures you are using).

Output:
>>> def user_input(): ... answer = input('Enter something: ') ... >>> user_input() Enter something: Have a nice day >>> answer Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'answer' is not defined >>>
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020