0% found this document useful (0 votes)
15 views2 pages

Python HW #2

Uploaded by

Orlando Robles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Python HW #2

Uploaded by

Orlando Robles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Python HW #2

1.

def calculator(num1, num2, operator):


if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 != 0:
result = num1 / num2
else:
return "Error: Division by zero is not allowed."
else:
return "Invalid operator. Please use +, -, *, or /."

return f"{num1} {operator} {num2} = {result}"

2.

def get_letter_grade(score):
if 90 <= score <= 100:
grade = 'A'
elif 80 <= score <= 89:
grade = 'B'
elif 70 <= score <= 79:
grade = 'C'
elif 60 <= score <= 69:
grade = 'D'
elif 0 <= score < 60:
grade = 'F'
else:
return "Invalid score. Please enter a score between 0 and 100."

return f"A score of {score} gives a {grade}."

3.

def wardrobe_advisory(temp_f):
if temp_f < 50:
advice = "Wear a jacket and warm layers."
elif 50 <= temp_f <= 70:
advice = "A light jacket or sweater is recommended."
elif 71 <= temp_f <= 90:
advice = "T-shirt and shorts are suitable."
else:
advice = "Stay hydrated and wear cool clothing."

return f"A temperature of {temp_f}°F suggests {advice}"

4.
def calculate_discount(price):
if price > 50:
discounted_price = price * 0.9 # Apply 10% discount
return f"The discount applies so the price is ${discounted_price:.2f}."
else:
return f"No discount so the price is ${price:.2f}."

5.

def classify_age(name, age):


if 0 <= age <= 12:
category = "child"
elif 13 <= age <= 17:
category = "teenager"
elif age >= 18:
category = "adult"
else:
return "Invalid age. Please enter a non-negative age."

return f"{name} is a {category} at the age of {age}."

You might also like