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.
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?
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.
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.
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
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.