Instructions
Objective
Write a program to create basic functions for computation in python language.
Requirements and Specifications
(Some of these exercises require written answers to questions. Answer those and copy and paste those answers to the bottom of the source file to which they apply).
This lab contains detailed information about functions as well as exercises using functions. Be sure to read through the entire lab. If you have any questions let me know.
Introduction to programmer-defined functions
We can write your python assignment on basic functions to perform some computations. These types of functions are referred to as programmer-defined functions. A programmer-defined function may have three parts: 1) Function definition, 2) Function Call, and/or 3) Function declaration.
Source Code
QUESTION 1
# Create function that takes temperature, wind speed and humidity
def getWeatherMessage(temp, humidity, windspeed):
F = temp
h = humidity
v = windspeed
weather_message = "Weather not known"
# First condition: good weather
# Temperature between 65 and 75, humidity below 50% and windspeed less than 10
if F >= 65 and F <= 75 and h < 50 and v < 10:
weather_message = "It is a good weather day"
# Second condition: bad weather
# Temperature above 85 or below 45, humidity above 80 or wind speed > 20
elif F > 85 or F < 45 or h > 80 or v > 20:
weather_message = "It is a bad weather day"
else:
weather_message = "It is just another day"
return weather_message
tempNow = float(input("What is the temperature in Fahrenheit?"))
humidityNow = float(input("What is the humidity in %?"))
windspeedNow = float(input("What is the windspeed in miles/hour?"))
weather = getWeatherMessage(tempNow, humidityNow, windspeedNow)
print(weather)
QUESTION 2
def isExpired(months, decayrate):
# Express decayrate as a ratio between 0 and 1
r = decayrate/100.0
# Now, create a loop from 1 to months
current_potency = 1
for i in range(months):
current_potency = current_potency*(1-r)
# if the potency is less than 0.5, the drug is expired
if current_potency < 0.5:
return True
else:
return False
# Test
print(isExpired(10,10)) # Should print True
print(isExpired(15,4)) # Should print False
QUESTION 3
def NominalGrade(Ascore, Escore):
final_score = (Ascore + Escore)/2.0 # Average between both scores
if final_score >= 86:
return 4
elif final_score >= 74 and final_score < 86:
return 3
elif final_score >= 62 and final_score < 74:
return 2
elif final_score >= 50 and final_score < 62:
return 1
elif final_score >= 0 and final_score < 50:
return 0
def TrueGrade(Ascore, Escore):
# Get NominalGrade
nominal = NominalGrade(Ascore, Escore)
# Get letter grade from exam only
exam_grade = NominalGrade(Escore, Escore)
# If the nominal is higher than exam_grade in 2 units, decrease nominal by 1
if nominal > exam_grade + 1:
nominal -= 1
letter_grades = ['F', 'D', 'C', 'B', 'A']
return letter_grades[nominal]
# Test
print(TrueGrade(96, 84)) # Should return A
print(TrueGrade(96,54)) # Should return C
QUESTION 4
def NumRepeats(num1, num2, num3):
# Add the items to a set. This will also work with integers
items = set([num1, num2, num3])
# Now, count the lenth of the set.
# If the set has length == 3, it means that all 3 items are different
if len(items) == 3:
return 0
# If the length of the set is equal to 2, it means one of the items is repeated
elif len(items) == 2:
return 1
# If the length of the set is equal to 1, it means that all items are equal
elif len(items) == 1:
return 2
# Test
print(NumRepeats(5, 9, 4)) # Should return 0
print(NumRepeats(5,9,5)) # Should return 1
print(NumRepeats(5,5,5))# Should return 2
# Test with strings
print(NumRepeats('A', 'B', 'C')) # Should return 0
print(NumRepeats('A', 'B', 'A')) # Should return 1
print(NumRepeats('A', 'A', 'A')) # Should return 2
Related Samples
Explore our free Python assignment samples to gain insights and improve your coding skills. These samples cover a variety of topics and include detailed explanations, making it easier for you to understand and apply Python concepts effectively. Check them out now to enhance your learning experience!
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python