Mathematical Program
print("Hello, welcome to the calculator!") #introduce user to calculator
while True: #if user does not input any incorrect syntax:
try: #if true, try:
firstnum = int(input("Choose one number: ")) #ask user for 1 number
secondnum = int(input("Choose another number: ")) #ask user for another number
operation = input("Please enter an operator (+, -, *, /): ") #ask what operator user wants to use
if operation not in ("+", "-", "*", "/"): #if user inputs incorrect operator:
print("Please enter a valid operator (+, -, *, /).")
continue
if operation == "+": #based on what operator user inputs,
print(firstnum + secondnum) #add firstnum and secondnum
elif operation == "-":
print(firstnum - secondnum) #subtract firstnum and secondnum
elif operation == "/":
if secondnum == 0:
print("Division by zero is not allowed.") #output error text if dividing by 0
else:
print(firstnum / secondnum) #divide firstnum and secondnum
elif operation == "*":
print(firstnum * secondnum) #multiply firstnum and secondnum
break
except ValueError: #if user does not input correct syntax for firstnum and secondnum
print("Please enter valid numbers.")
Hello, welcome to the calculator!
Choose one number: 4
Choose another number: 5
Please enter an operator (+, -, *, /): /
0.8
Chatgpt Improved Code
# Improved chatgpt code:
# Prompt the user for input and strip any leading/trailing whitespace
var1 = input("Is Mr. Mort the best teacher ever? ").strip().lower()
# Use a dictionary to store the responses
responses = {
"yes": "You are correct.",
"no": "You are wrong.",
}
# Check if the user's input is a valid key in the responses dictionary
if var1 in responses:
print(responses[var1])
else:
print("Please enter a valid response (yes or no).")
Is Mr. Mort the best teacher ever? maybe
Please enter a valid response (yes or no).
Final Project!
Recipe book manager
# Define the Recipe class to store details about a recipe
class Recipe:
# Constructor to initialize a Recipe object
def __init__(self, name, ingredients, instructions):
self.name = name
self.ingredients = ingredients
self.instructions = instructions
# Method to display the recipe details
def display(self):
print(f"Recipe Name: {self.name}")
print("\nIngredients:")
for ingredient in self.ingredients:
print(f"- {ingredient}")
print("\nInstructions:")
for instruction in self.instructions:
print(f"- {instruction}")
# Function to search for a recipe by name from a list of recipes
def find_recipe(name, recipes):
for recipe in recipes:
if recipe.name.lower() == name.lower():
return recipe
return None
# Function to delete a recipe by name from a list of recipes
def delete_recipe(name, recipes):
for index, recipe in enumerate(recipes):
if recipe.name.lower() == name.lower():
del recipes[index]
return True
return False
# List to store all the recipes
recipes = []
# Main loop for the Recipe Book Manager
while True:
print("\nWelcome to the Recipe Book Manager")
print("Select your action")
print("A: Add a recipe \nB. View a Recipe \nC. Delete a Recipe \nD. Exit")
choice = input("Your Choice: ").upper()
# Add a new recipe
if choice == "A":
recipe_name = input("Enter the name of the recipe: ")
ingredients = []
while True:
ingredient = input("Add an ingredient (or type 'done' to finish): ")
if ingredient.lower() == 'done':
break
ingredients.append(ingredient)
instructions = []
while True:
instruction = input("Enter the process/instructions for the recipe (Type 'done' to finish): ")
if instruction.lower() == "done":
break
instructions.append(instruction)
new_recipe = Recipe(recipe_name, ingredients, instructions)
recipes.append(new_recipe)
print("Recipe added successfully!")
new_recipe.display()
# View a specific recipe
elif choice == "B":
recipe_name = input("Enter the name of the recipe you want to view: ")
recipe = find_recipe(recipe_name, recipes)
if recipe:
recipe.display()
else:
print("Recipe not found!")
# Edit or Delete a specific recipe
elif choice == "C":
action = input("Would you like to Edit (E) or Delete (D) a recipe? ").upper()
if action == "D":
recipe_name = input("Enter the name of the recipe you want to delete: ")
if delete_recipe(recipe_name, recipes):
print("Recipe deleted successfully!")
else:
print("Recipe not found!")
# Exit the Recipe Book Manager
elif choice == "D":
print("Goodbye!")
break
Welcome to the Recipe Book Manager
Select your action
A: Add a recipe
B. View a Recipe
C. Delete a Recipe
D. Exit
Your Choice: A