5.2 step 15, question with the use of "def"

In 5.2 why did they use a definition in the program like below,

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

def find_percentage():

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

I did it without like this and it seems to work just as well.

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) + “%”)

Am I missing something?

1 Like

The code “def” is used to define a function. I think what we are learning here is that, if, in a code, we define a particular function, we can then “call” it for many different variables. It’s probably going to be made clear why we would do that later on in programming when we create longer and more complex programs.

You can always work out the guts of a function each time, but if you define it, you can “call”, or use, it again and again.

1 Like