Q1: Write a program that prompts the user for a meal: breakfast, lunch, or dinner. Then using if statements and else statements print the user a message recommending a meal. For example, if the meal was breakfast, you could say something like, “How about some bacon and eggs?” The user may enter something else in, but you only have to respond to breakfast, lunch, or dinner
# Prompt the user for a meal choice
meal_choice = input("What meal are you planning to have (breakfast, lunch, or dinner)? ")
# Provide recommendations based on the meal choice
if meal_choice == "breakfast":
print("How about some pancake and eggs?")
elif meal_choice == "lunch":
print("Would you like to have a sandwich or a salad.")
elif meal_choice == "dinner":
print("Consider having some grilled chicken or pasta.")
else:
print("Sorry, I can only recommend meals for breakfast, lunch, or dinner.")
What meal are you planning to have (breakfast, lunch, or dinner)? dinner Consider having some grilled chicken or pasta.
Q2: The mailroom has asked you to design a simple payroll program that calculates a student employee’s gross pay, including any overtime wages. If any employee works over 20 hours in a week, the mailroom pays them 1.5 times their regular hourly pay rate for all hours over 20. You should take in the user’s input for the number of hours worked, and their rate of pay.
# Get user input for hours worked and hourly pay rate
hours_worked = float(input("Enter the number of hours worked: "))
hourly_pay_rate = float(input("Enter the hourly pay rate: "))
# Calculate regular pay for the first 20 hours
if hours_worked <= 20:
regular_pay = hours_worked * hourly_pay_rate
overtime_pay = 0 # No overtime if hours worked are 20 or less
else:
regular_pay = 20 * hourly_pay_rate
overtime_hours = hours_worked - 20
overtime_pay = overtime_hours * 1.5 * hourly_pay_rate
# Calculate total gross pay
gross_pay = regular_pay + overtime_pay
# Display the results
print(f"Regular Pay: ${regular_pay:.2f}")
print(f"Overtime Pay: ${overtime_pay:.2f}")
print(f"Total Gross Pay: ${gross_pay:.2f}")
Enter the number of hours worked: 498 Enter the hourly pay rate: 294 Regular Pay: $5880.00 Overtime Pay: $210798.00 Total Gross Pay: $216678.00
Q3: Write a function named times_ten. The function should accept an argument and display the product of its argument multiplied times 10.
def times_ten(argument):
print(argument*10)
times_ten(10)
100
Q4: Find the errors, debug the program, and then execute to show the output.
def main()
Calories1 = input( "How many calories are in the first food?")
Calories2 = input( "How many calories are in the first food?")
showCalories(calories1, calories2)
def showCalories()
print(“The total calories you ate today”, format(calories1 + calories2,.2f))
Adjusted the indentation in the main function, moving it two steps back, as Python is sensitive to indentation levels.
Modified the input prompt for calories2 to specify the second food.
Capitalized the variable names calories in the showCalories function to match Python's case sensitivity.
Converted the calories input to integers since the input function returns strings.
Corrected the usage of the format method, removing the '.2f' to properly format the total calories.
Replaced non-standard quotation marks with standard double quotes to define string literals.
Added colons at the end of the function definitions for proper syntax.
Called the main() function to execute the program.
Added arguments to the showCalories function to accept calories1 and calories2.
def main():
Calories1 = input("How many calories are in the first food? ")
Calories2 = input("How many calories are in the second food? ")
showCalories(int(Calories1), int(Calories2))
def showCalories(Calories1,Calories2):
print("The total calories you ate today {:.2f}".format(Calories1 + Calories2))
main()
How many calories are in the first food? 25 How many calories are in the second food? 18 The total calories you ate today 43.00
Q5: Write a program that uses any loop (while or for) that calculates the total of the following series of numbers:
1/30 + 2/29 + 3/28 ............. + 30/1
def sum_of_fractions(min,max):
summation = 0
for x in range(min,max+1):
summation += (x)/(max-(x-1))
return summation
min = 1
max = 30
summation = sum_of_fractions(min,max)
print(summation)
print(round(summation,2))
93.84460105853213 93.84
Q6: Write a function that computes the area of a triangle given its base and height. The formula for an area of a triangle is:
AREA = 1/2 BASE HEIGHT
For example, if the base was 5 and the height was 4, the area would be 10. triangle_area(5, 4) # should print 10
# The function below can be modified to only display the area
def triangle_calc(base = 0,height = 0):
if base == 0 and height == 0:
base = float(input("Please enter the base of your triangle: "))
height = float(input("Please enter the height of your triangle: "))
area = (1/2)*base*height
print(f"A {base} by {height} triangle has an area of {area}")
triangle_calc(5,4)
triangle_calc()
A 5 by 4 triangle has an area of 10.0 Please enter the base of your triangle: 25 Please enter the height of your triangle: 126 A 25.0 by 126.0 triangle has an area of 1575.0