Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Defining Functions
#1
Hello, I am struggling to define a function and I'm finding my reading material isn't really helping or explaining correctly. I'm not looking for the complete answer to my question. Just some general pointers in the right direction.

The question:

Let us suppose a 5 GHz radio wave that:

propagates through the atmosphere in rainy weather over a distance d, in kilometres (km)
and then through one layer of wood of thickness t, in metres (m).
The problem
The problem is to write a function to calculate the total power loss, measured in dB (decibel), under those circumstances given that:

rain leads to an atmospheric loss of 1.5 decibel per kilometre (dB/km)
wood leads to a further loss of 90 decibel per metre (dB/m)
to account for other unknown noise and impairments that might be present, the combined loss (obtained by adding the loss from 1 and 2) is multiplied by your individual number 1.0XYZ. This gives you the total loss.
You can test your approach with typical values of d = 30 km and t = 0.1 m (which should result in a combined loss of 54 dB, which must then be multiplied by your individual number in order to find the total loss).

The function should have two arguments: one for the distance d and one for the thickness t of the layer of wood.
The function should have one return value for the total power loss.

def decibel_test(atmospheric_loss,decibel_loss,total_loss,rain,unique_number):
    
    """calculate the loss in decibels over a pre determined distance"""
    atmospheric_loss = rain * distance
    decibel_loss = timber_loss + atmospheric_loss
    total_loss = decibel_loss * unique_number
    unique_number = 1.01993
    rain = 1.5
    
    
    
print('distance in km')
distance=float(input())
print('wood in metres')
timber_loss = 0.1 * float(input())
DB_loss=decibel_test(distance,timber_loss,rain,unique_number)




print('the total decibel loss is', DB_loss)

    

    
Error:
Traceback (most recent call last): File "C:\Users\Andrew\OneDrive - The Open University\TM112\TMA02\Question 4.py", line 16, in <module> DB_loss=decibel_test(distance,timber_loss,rain,unique_number) NameError: name 'rain' is not defined
If I fix the rain is not defined issue, I know I'll end up coming across more. This is about as far as I can get with the materials provided.
Reply
#2
If you define rain inside decibel_test, why you pass it as a parameter.
BTW the value rain that you pass into function isn't defined.
Reply
#3
Order matters.
You use rain in line 4 but do not define the value until line 8. Python will not look ahead - a program is a structured process. Likewise you cannot use unique_number in line 6 before you define its value in line 7.

Also, like above, you call the function using 4 parameters, but you have only defined the first two at that point.

So, best would be to move lines 7 and 8 outside of the function to just above line 16 where the function is called.
Reply
#4
Thank you very much for the replys. I have now made the below changes and my function is now working as intended.

def decibel_test(distance, timber_loss):
    
    """calculate the loss in decibels over a pre determined distance"""
    unique_number = 1.01993
    rain_loss = 1.5
    atmospheric_loss = rain_loss * distance
    decibel_loss = timber_loss + atmospheric_loss
    total_loss = decibel_loss * unique_number
    return total_loss
    
    
    
    
    
    
print('distance in km')
distance=float(input())
print('wood in metres')
timber_loss = 90 * float(input())
DB_loss=decibel_test(distance,timber_loss)




print('the total decibel loss is', DB_loss)

    

    
Reply
#5
It is difficult to answer a question like this through hints. A function should look like this:
def func(arg1, arg2):
    body
    return value
func is the name of the function. It should be descriptive. decibel_test is not a descriptive name. The function calculates a decibel loss, so that is the name for you function.
def db_loss(arg1, arg2):  # Do not use upper in function names even if the words would use upper case
    body
    return value
arg1, arg2 are function arguments. Function arguments are used to pass values into a function. The arguments you need to pass to your function are your input values; distance and timber.
def db_loss(distance, timber):
    body
    return value
The function returns a value using "return". Your function should return the decibel loss calculated using the distance and timber input values.
def db_loss(distance, timber):
    loss = (distance * 1.5 + timber * 0.1) * 1.01993
    return loss
Your function can create local variables to be used in calculations like "loss" in the example above. This variable is only visible within the scope of the function. "loss" is not visible outside the function. You could also write the db_loss without any local variables.
def db_loss(distance, timber):
    return (distance * 1.5 + timber * 0.1) * 1.01993
1.5, 0.1 and 1.01993 are "magic numbers", numerical values that must have some meaning, but the meaning is not documented in any way. How you handle these numbers depends on the characteristics of the number. If a number is constant, you might want to make it a global variable. Global variables should be all upper case letters.
RAIN_LOSS_DB_PER_KM = 1.5
TIMBER_LOSS_DB_PER_M = 0.1

def decibel_test(distance, timber):
    """calculate the loss in decibels over a pre determined distance"""
    return (distance * RAIN_LOSS_DB_PER_KM + timber * TIMBER_LOSS_DB_PER_M) * 1.01993
If a value is variable, but is usually the same value, you could pass it as an argument with a default value.
RAIN_LOSS_DB_PER_KM = 1.5
TIMBER_LOSS_DB_PER_M = 0.1

def decibel_test(distance, timber, unique_number=1.01993):
    """calculate the loss in decibels over a pre determined distance"""
    return (distance * RAIN_LOSS_DB_PER_KM + timber * TIMBER_LOSS_DB_PER_M) * unique_number
When mixing arguments that have default values with those that don't, the arguments with default values have to appear after any that doesn't. The arguments without default values are called "positional" parameters and the arguments with default values are "keyword" or "keyword value" parameters.
FYI, the input() function takes a prompt so you don't need a separate print command.
RAIN_LOSS_DB_PER_KM = 1.5
TIMBER_LOSS_DB_PER_M = 0.1

def decibel_test(distance, timber, unique_number=1.01993):
    """calculate the loss in decibels over a pre determined distance"""
    return (distance * RAIN_LOSS_DB_PER_KM + timber * TIMBER_LOSS_DB_PER_M) * unique_number

distance = float(input('distance in km '))
timber = float(input('wood in metres '))
print('The total decibel loss is', decibel_test(distance, timber))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question trouble with functions "def", calling/defining them Duck_Boom 13 4,598 Oct-21-2020, 03:50 AM
Last Post: Duck_Boom
  Defining multiple functions in the same def process sparkt 5 2,950 Aug-09-2020, 06:19 PM
Last Post: sparkt
  Defining functions TommyAutomagically 1 1,932 Apr-25-2019, 06:33 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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