functions and exceptions in python codehs
6.3.5: Fix This Program!
""" This program takes two values and prints their sum. """ def add_nums(num1, num2): sum = num1 + num2 add_nums(5, 6) num1=5 num2=6 print(sum)
6.4.9: Temperature Converter
# Write your function for converting Celsius to Fahrenheit here. # Make sure to include a comment at the top that says what # each function d # Now write your function for converting Fahrenheit to Celsius. def celcius_to_fahrenheit(celcius): return celcius * 1.8 + 32 # Now change 0C to F: print ("0C In F: " + str(celcius_to_fahrenheit(0))) # Change 100C to F: print ("100C In F: " + str(celcius_to_fahrenheit(100))) # Change 40F to C: print ("40F In C: " + str(fahrenheit_to_celcius(40))) # Change 80F to C: print ("80F In C: " + str(fahrenheit_to_celcius(80)))
6.2.8: Area of a Square with default paremeters
def calculate_area(side_length = 10): calculate_area = side_length ** 2 print("The area of a square with sides of length " + str(side_length) + "is" + str(calculate_area) + ".") side_length = int(input("How many sides? ")) if side_length <= 0: calculate_area() else: calculate_area(side_length)
6.5.5: Temperature Converter part 2
def fahrenheit_to_celcius(fahrenheit): return (fahrenheit - 32) / 1.8 # Now write your function for converting Fahrenheit to Celsius. def celcius_to_fahrenheit(celcius): return celcius * 1.8 + 32 #ask te user to enter a temperature try: temperature = int(input("Enter a temp: ")) except ValueError: print("That wasn't an integer.") # Now change 0C to F: print ("0C In F: " + str(celcius_to_fahrenheit(0))) # Change 100C to F: print ("100C In F: " + str(celcius_to_fahrenheit(100))) # Change 40F to C: print ("40F In C: " + str(fahrenheit_to_celcius(40))) # Change 80F to C: print ("80F In C: " + str(fahrenheit_to_celcius(80)))
6.1.5: Weather
def is_sunny(): print("On a sunny day, sandals are appropriate footwear.") def is_rainy(): print("On a rainy day, galoshes are appropriate footwear.") def is_snowy(): print("On a snowy day, boots are appropriate footwear.") weather_outside= input("Whats the weather like? (sunny, rainy, snowy): ") if weather_outside == "sunny": is_sunny() elif weather_outside == "rainy": print("On a rainy day, galoshes are appropriate footwear.") elif weather_outside == "snowy": print("On a snowy day, boots are appropriate footwear.") else: print("Invalid option.")
6.2.6: Print Multiple Times
def print_multiple_times(x,y): print(x) print(y) print_multiple_times(4,5) print_multiple_times("hi, maybe")
6.2.5: Print Product
def print_value(x,y): print(x) print(y) print_value(12,4) print_value("dog")
6.4.4: Add One
def return_ten(): return 10 print(return_ten()) x = return_ten() print(x + 1)
6.4.8: Sum Two Numbers
def return_ten(4,8): return 10 print(return_ten()) x = return_ten() print(x + 1)
6.5.6: Enter a Positive Number
name = input("Enter your name: ") age = -1 try: age = int(input("Enter your age: ")) except ValueError: print("That wasn't an integer.") # Print name and age, using default age if user did not enter an integer print("Name: " + name) print("Age: " + str(age))
6.3.6: Adding to a Value
num1 = 10 """ This program takes two values and prints their sum. """ def print_something(): num1= "hi" print(num1* 3) print_something() print(num1* 3)
6.3.7: Add, Subtract, or Multiply
num1 = int(input("What is the first number?: ")) num2 = int(input("What is the second number?: ")) def add(): sum = num1+num2 print(str(num1) + " + " +str(num2) + " = " + str(sum)) def subtract(): sum = num1-num2 print(str(num1) + " - " +str(num2) + " = " + str(sum)) def multiply(): sum = num1*num2 print(str(num1) + " * " +str(num2) + " = " + str(sum)) def invalid(): print("An invalid option was selected") Operation = str(input("Choose an operation (add, subtract, multiply): ")) if(Operation=="add"): add() elif(Operation=="subtract"): subtract() elif(Operation=="multiply"): multiply() else: invalid()