# Module 5.2 step 16

I am stuck on this step. I think I missed what a return means somewhere, but I went back through a couple modules and did not see where it defined what a return was. I honestly do not understand how to setup the syntax on this step at all. I understand how to find a percentage. Here is what I have so far and what error I am getting.

def find_percentage():
percentage = (passing_students / total_students) * 100

print("The percentage of students who passed is " + str(percentage) + “%”)

RESULTS:
How many students are in the class? 25
How many students passed this test? 7
Traceback (most recent call last):
File “python”, line 7, in
NameError: name ‘percentage’ is not defined

The error says that percentage is not defined, but I did define it above. What am I doing wrong here?

I was getting an error for a while and finally got it to work.

total_students = int(input(“How many students are in the class?”))
passing_students = int(input(“How many students passed this test?”))

percentage = passing_students/total_students * 100
print("The percentage of students who passed is " + str(percentage) + “%”)

This is the result of the code working below. The difference I notice is the parenthesis. Hope this makes sense.

40
12
How many students are in the class?How many students passed this test?The percentage of students who passed is 30.0%
=> None

Maggie

You do indeed need to ‘return’ the percentage. The return function tells the rest of the code what it has found. It shares the answer of the calculation, so the rest of the code can use this information.

I guess I don’t even understand how to setup the return. I tried Maggie’s suggestion, but I still get the percentage not defined error.

def find_percentage(passing_students, total_students):
percentage = (passing_students/total_students) * 100

percentage = int(find_percentage() * 100)
print("The percentage of students who passed is " + str(percentage) + “%”)

ERROR:
Traceback (most recent call last):
File “python”, line 8, in
TypeError: find_percentage() missing 2 required positional arguments: ‘passing_students’ and ‘total_students’

I tried putting those arguments in the (), but it still throws back an error saying I’m missing passing_students. I am very confused in this module.

I finally figured it out! Ugh. Just needed a little confidence.

Since you calculate the percentage in the function, why not just code a new line, “return percentage”?

The rest of the code needs to call the function and print the results. To call, you need to specify your two variables. Then you can code to print the return variable.

There should be some examples in the lessons.

Celeste-
Here is the basic set up of a function and, just as importantly(!), a call to that function:

def a_function (a comma-separated parameter list):
variable_name = do some math here using the parameter(s)

`````` return variable_name
``````

The following is not part of the function, it’s the main body of your program and includes a call to the function. You can set it up in a main() function if you’d like, but at this point it isn’t necessary. If you DO set it up in main(), be sure to call main() as the last line of your code or nothing will happen

a_variable = input(“Ask for input here”)
another_variable = input(“Ask for input here”)

main_body_result_variable = a_function(comma-separated argument list)
print(“Any text you like” + main_body_result_variable)

Think that structure through carefully and the solution to your exercise should become apparent to you. Note that some of the code could actually be condensed a little, but don’t worry about that right now–work on understanding 1) the structure of a function, 2) what it returns 3) how it returns it, 4) how to call that function, 5) how the main body of code can receive/store the returned value, and 6) how/where to output that value

Best wishes for success. Don’t.Give.Up.

MarkB

1 Like

Thank you Mark! That was extremely helpful information!

1 Like

Hello,

I am also having issues with this problem

You are thinking in the right direction.

However, look VERY carefully at the structure of the line. The two editable fields are separated by what? What does that signify? Wouldn’t it mean that you must divide the first field (the dividend) by the second field (the divisor)?

If that is the case, reconsider what is in the first field. It looks like you entered a variable (percentage), the assignment operator (=), and one of the function parameters (passing_students). That’s too much information for it to handle!

Keep it simple! You have two parameters that have been passed into the function. Using those two parameters (which already have names), how would you set up a simple division problem that would “find_percentage”?

You already understand the problem and the logic required to solve it. Now beat the syntax into submission! Oh, and Don’t.Give.Up.

1 Like