I am currently doing an online coding course using python. It has asked me to finish writing the partially written out code it has given me... but has thrown in the 'def' function but not taught it to me. I managed to write the code to give the answer it is after without using the 'def' function...
deathsInSA = 25000
deathsInM = 18000
deathsInA = 6900
deathsInEG = 67
deathsInGB = 1200
largest = max(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB)
smallest = min(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB)
rangeOfDeaths = largest - smallest
print(rangeOfDeaths)`
24933
...but the course will not let me pass without using the 'def' function. Please help! I have tried to solve it but nothing works. I don't particularly understand what the point of the def function is if I can come to the correct result without it?
Exercise: You have been given a partially completed Python function called rangeOfDeaths to calculate the range of the deaths which should return the range of deaths in 5 African countries. These deaths in each country have already been assigned for you to variables: deathsInSA, deathsInM, deathsInA, deathsInEG and deathsInGB.
Add the additional lines of code necessary for result to hold the difference between the largest and smallest number of deaths. This should be a positive number.
def rangeOfDeaths():
deathsInSA = 25000
deathsInM = 18000
deathsInA = 6900
deathsInEG = 67
deathsInGB = 1200
return result
CodePudding user response:
As per your definition, those are given
deathsInSA = 25000
deathsInM = 18000
deathsInA = 6900
deathsInEG = 67
deathsInGB = 1200
The function to calculate the range of deaths can be
def range_of_deaths(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB):
largest = max(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB)
smallest = min(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB)
return largest - smallest
Now, outside the function, call it
print(rangeOfDeaths(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB))
CodePudding user response:
result is returned by the function. So, you have to assign your variable rangeOfDeaths or directly
largest - smallest to it:
def rangeOfDeaths():
deathsInSA = 25000
deathsInM = 18000
deathsInA = 6900
deathsInEG = 67
deathsInGB = 1200
largest = max(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB)
smallest = min(deathsInSA, deathsInM, deathsInA, deathsInEG, deathsInGB)
result = largest - smallest
return result
