# Noemi Cabrera
# 1 October 2021
# In this lab, I practiced using if/else Conditionals along with elif statement, casting from one data type to
# another, gathering numeric input using type casting, and performing subtraction, multiplication and
# division operations in code
# I did not have any difficulties in this lab
# [ In this code, the user enters input on the favorite rainbow color. The function returns the letter and color
# it matches with if the letter entered is on the list. If the letter is not on the list, "no match" will print.]
# complete rainbow colors
fav_color= input("Enter the first letter of your favorite rainbow color (ROYGBIV):").upper()
if fav_color == "R":
print("R = Red")
elif fav_color == "O":
print("O = Orange")
elif fav_color == "Y":
print("Y = Yellow")
elif fav_color == "G":
print("G = Green")
elif fav_color == "B":
print("B = Blue")
elif fav_color == "I":
print("I = Indigo")
elif fav_color == "V":
print("V = Violet")
else:
print("no match")
# [In the defined function, the user enters input on the favorite rainbow color. The function returns the letter
# and color that matches with the user input. If the letter is not on the list, "no match" will print.]
# make the code above into a function rainbow_color() that has a string parameter,
# get input and call the function
def rainbow_color(color):
fav_color= input("Enter the first letter of your favorite rainbow color (ROYGBIV): ").upper()
if fav_color == "R":
return("R = Red")
elif fav_color == "O":
return("O = Orange")
elif fav_color == "Y":
return("Y = Yellow")
elif fav_color == "G":
return("G = Green")
elif fav_color == "B":
return("B = Blue")
elif fav_color == "I":
return("I = Indigo")
elif fav_color == "V":
return("V = Violet")
else:
return("no match")
print("Your color:")
print(rainbow_color("O"))
# [ In this code, the user enters input on their age. If their age is less than 20, the function returns their
# age plus 20. If their age is greater than 20, the function returns their age minus 20. ] complete age_20()
def age_20():
current_age = int(input("Enter your age:"))
if current_age > 20:
return current_age - 20
else:
return current_age + 20
if age_20() > 20:
print(age_20(),"years old","- 20 years more from now")
else:
print(age_20,"years old","- 20 years difference from now")
# [ This function displays either the age_20() or rainbow_color() functions created previously.
# The argument that is inserted when calling the function rainbow_or_age() determines which of the previous
# functions will execute. ] create rainbow_or_age()
def rainbow_or_age(value):
if value.isdigit():
return int(age_20())
elif value.isalpha():
return rainbow_color("g")
else:
return False
rainbow_or_age("28")
# [ This code represents an addition calculator. The user is asked to enter 2 numbers that are added,
# and the result is displayed. ] add 2 numbers from input using a cast to integer and display the answer
print("This is an addition calculator")
num_1= int(input("Enter a number:"))
num_2= int(input("Enter a second number:"))
total= num_1 + num_2
print(num_1,"+",num_2,"=", total)
# [ This code represents a multiplying calculator. The user is asked to enter 2 numbers that are multiplied,
# and the answer is displayed. ] Multiply 2 numbers from input using cast and save the answer as part of a
# string "the answer is..."
# display the string using print
print("This is an multiplying calculator")
one= int(input("Enter a number:"))
two= int(input("Enter a second number:"))
result= one * two
print("The answer is", result)
# [ This code represents an average calculator. The user is asked to enter 2 numbers that are added and divided
# by 2 to get the average. The result is displayed. ] get input of 2 numbers and display the average:
# (num1 + num2) divided by 2
print("This is an average calculator")
value1 = int(input("Enter a number:"))
value2 = int(input("Enter a second number:"))
average= (value1 + value2)/2
print("The average is", average)
# [ In this code, the user is asked to enter 2 numbers. If value1 is greater than value2, then the substraction
# of value 2 - value1 is displayed. But if it's not, the else statement will display .]
# get input of 2 numbers and subtract the largest from the smallest (use an if statement to see which is larger)
# show the answer
value1 = int(input("Enter a number:"))
value2 = int(input("Enter a second number:"))
if value1 > value2:
print(value2 - value1)
else:
print(value1 - value2)
# [ The numbers are stored in variables. If the small number is equal to 0, the result"0" will display.
# But if it's not 0, the division between the two will display. ]
# Divide a larger number by a smaller number and print the integer part of the result
# don't divide by zero! if a zero is input make the result zero
# [ To cast the answer as integer the int() function is used.] cast the answer to an integer to cut
# off the decimals and print the result
l_num = 12
s_num = 6
if s_num == 0:
print(0)
else:
division = l_num / s_num
print(int(division))