def get_shirt_size(measurement):
if measurement > 32:
return "small"
if measurement > 36:
return "Medium"
if measurement > 40:
return "Large"
if measurement > 44:
return "X-Large"
measurement = float(input("Enter your chest measurement (in inches): "))
shirt_size = get_shirt_size(measurement)
print(f"Based on your measurement, your shirt size is: {shirt_size}")
Based on your measurement, your shirt size is: small
def get_shirt_size(measurement):
if measurement < 36:
return "Small"
elif measurement < 40:
return "Medium"
elif measurement < 44:
return "Large"
else:
return "X-Large"
measurement = float(input("Enter your chest measurement (in inches): "))
shirt_size = get_shirt_size(measurement)
print(f"Based on your measurement, your shirt size is: {shirt_size}")
Based on your measurement, your shirt size is: Medium
original code
def divide_nums(a, b):
# divide
result = a / b
return result
# test with numbers
def run_tests():
# test case 1 : valid
result1 = divide_nums(10, 2)
print("Test 1 result:", result1)
# test case 2: not valid
result2 = divide_nums(5, 0)
print("Test 2 result:", result2)
# run tests
run_tests()
Test 1 result: 5.0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb Cell 5 line 1
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> print("Test 2 result:", result2)
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a> # run tests
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=17'>18</a> run_tests()
/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb Cell 5 line 1
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a> print("Test 1 result:", result1)
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a> # test case 2: not valid
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a> result2 = divide_nums(5, 0)
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> print("Test 2 result:", result2)
/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb Cell 5 line 4
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> def divide_nums(a, b):
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a>
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> # divide
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> result = a / b
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/ellier/vscode/csp3-repo/_notebooks/2023-09-20-team-test-vid-hacks.ipynb#X20sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a> return result
ZeroDivisionError: division by zero
testing using if statements
def divide_nums(a, b):
# check dividing by zero
if b == 0:
return "Error: Cannot divide by zero"
# divide
result = a / b
return result
# test with numbers
def run_tests():
# test case 1 : valid
result1 = divide_nums(10, 2)
print("Test 1 result:", result1)
# test case 2: not valid
result2 = divide_nums(5, 0)
print("Test 2 result:", result2)
# run tests
run_tests()
Test 1 result: 5.0
Test 2 result: Error: Cannot divide by zero
try and except (catch) statements
def divide_nums(a, b):
try:
# Attempt division
result = a / b
return result
except ZeroDivisionError:
return "Error: Cannot divide by zero"
except TypeError:
return "Error: Please provide numerical inputs"
# Test the function
def run_tests():
# Test case 1: Valid division
try:
result1 = divide_nums(10, 2)
print("Test 1 result:", result1)
except Exception as e:
print("An error occurred:", str(e))
# Test case 2: Division by zero
try:
result2 = divide_nums(5, 0)
print("Test 2 result:", result2)
except Exception as e:
print("An error occurred:", str(e))
# Run the tests
run_tests()
Test 1 result: 5.0
Test 2 result: Error: Cannot divide by zero