Teamtest_ipynb_2_
September 2023 (985 Words, 6 Minutes)
Program with Output:
- Simple print line
print("This is a Jupyter Notebook made for the team test")
Program with Input/Output:
- Program to prompt user to input name and generate a greeting
user_name = input("Please enter your name: ")
print(f"Hello, {user_name}! Welcome to our notebook.")
Program with list:
• Simple function to display a shopping list with bullets
shopping_list = ["Milk", "Bread", "Onions", "Oil"]
print("Shopping List:")
for item in shopping_list:
print("• " + item)
Program with a dictionary:
• Dictionary to display different aspects of a book
book = {"name": "Harry Potter", "author": "J. K. Rowling", "description": "Harry Potter is a young wizard destined for greatness in the magical world."}
print("Name:", book["name"])
print("Author:", book["author"])
print("Description:", book["description"])
Program with iteration
• Simple loop that prints numbers in a loop to generate a pyramid shape out of numbers
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + " ".join(str(j) for j in range(1, i * 2)))
Program with a function
• Simple addition function
def calculate_sum(a, b):
return a + b
print("Pick two numbers to be added together")
a = int(input("Pick number one: "))
b = int(input("Pick number two: "))
result = calculate_sum(a, b)
print("Number one:", a)
print("Number two:", b)
print("Sum:", result)
Program with a Selection/Condition
• Program that takes user input on weather and displays what you should do using the if, elif, and else functions
print("Enter the weather type right now for advice")
weather = input("Enter the weather (sunny, rainy): ").lower()
if weather == "sunny":
print("If the weather is sunny, enjoy the sun and use sunscreen")
elif weather == "rainy":
print("If the weather is rainy, try to stay inside but if not, use an umbrella and wear a poncho")
else:
print("Sorry, ", weather, "was not one of the options. Please try again")
Program with purpose:
• talk about this later? a little confusing
def calculate_tax(amount, tax_rate):
return amount * (tax_rate / 100)
try:
tax_rate = int(input("Enter the tax rate (in whole numbers, e.g., 7 for 7%): "))
amount_spent = float(input("Enter the amount spent: $"))
if tax_rate < 0 or amount_spent < 0:
print("Tax rate and amount spent cannot be negative.")
else:
tax_amount = calculate_tax(amount_spent, tax_rate)
total_amount = amount_spent + tax_amount
print(f"Tax amount: ${tax_amount:.2f}")
print(f"Total amount after tax: ${total_amount:.2f}")
except ValueError:
print("Invalid input. Please enter valid numbers for tax rate and amount spent.")
Tester function
First import that math module to take square roots
import math
Create a function that takes a square root of a number. However, there is an error where the code doesn’t know what to do if the number is negative as square roots of negative numbers are not real and it can cause issues.
def calculate_square_root(num):
return math.sqrt(num)
Start debugging the function with try, except, if, and else. Then give the function two numbers. One negative and one positive. The negative will print an error but the positive will print a real number
def test_calculate_square_root(num):
try:
if num < 0:
raise ValueError("Cannot calculate square root for a negative number")
else:
print(f"The square root of {num} is: {math.sqrt(num)}")
except ValueError as e:
print(f"Error: {e}")
# Test and debug function
test_calculate_square_root(-9)
test_calculate_square_root(25)
Correct the function to explain the error
def calculate_square_root(num):
try:
if num < 0:
raise ValueError(f"Cannot calculate square root for {num} as it is a negative number.")
else:
print(f"The square root of {num} is: {math.sqrt(num)}")
except ValueError as e:
print(f"{e}")
return None
# Test the final function
calculate_square_root(-9)
calculate_square_root(25)