# [ ] complete rainbow colors
favorite_rainbow_color = input("What is the first letter of your favorite rainbow color? ROYGBIV")
if favorite_rainbow_color == "R":
print("Red")
elif favorite_rainbow_color == "O":
print("Orange")
elif favorite_rainbow_color == "Y":
print("Yellow")
elif favorite_rainbow_color == "G":
print("Green")
elif favorite_rainbow_color == "B":
print("Blue")
elif favorite_rainbow_color == "I":
print("Indigo")
elif favorite_rainbow_color == "V":
print("Violet")
else:
print("no match")
# [ ] make the code above into a function rainbow_color() that has a string parameter,
# get input and call the function and return the
def rainbow_color():
favorite_rainbow_color = input("What is your favorite rainbow color?")
if favorite_rainbow_color == "R":
print("Red")
elif favorite_rainbow_color == "O":
print("Orange")
elif favorite_rainbow_color == "Y":
print("Yellow")
elif favorite_rainbow_color == "G":
print("Green")
elif favorite_rainbow_color == "B":
print("Blue")
elif favorite_rainbow_color == "I":
print("Indigo")
elif favorite_rainbow_color == "V":
print("Violet")
else:
print("no match")
rainbow_color()
# [ ] complete age_20()
def age_20(age):
add_age = age + 20
subtract_age = age - 20
if subtract_age>0:
return subtract_age
elif add_age>0:
return add_age
else:
return "invalid age"
print(age_20(-30) ,"years old, 20 years difference from now")
# [ ] create rainbow_or_age()
def age_20(age):
add_age = age + 20
subtract_age = age - 20
if subtract_age>0:
return subtract_age
elif add_age>0:
return add_age
else:
return "invalid age"
def rainbow_color(favorite_rainbow_color):
if favorite_rainbow_color == "R":
print("Red")
elif favorite_rainbow_color == "O":
print("Orange")
elif favorite_rainbow_color == "Y":
print("Yellow")
elif favorite_rainbow_color == "G":
print("Green")
elif favorite_rainbow_color == "B":
print("Blue")
elif favorite_rainbow_color == "I":
print("Indigo")
elif favorite_rainbow_color == "V":
print("Violet")
else:
print("no match")
def rainbow_or_age(per):
if per.isdigit():
return age_20(int(per))
elif per.isalpha():
return rainbow_color(per)
else:
return False
rainbow_or_age("20")
# [ ] add 2 numbers from input using a cast to integer and display the answer
num_1 = input("enter a number")
num_2 = input("enter a second number")
sum = int(num_1) + int(num_2)
print(sum)
# [ ] Multiply 2 numbers from input using cast and save the answer as part of a string "the answer is..."
# display the string using print
num_1 = input("enter a number")
num_2 = input("enter a second number")
product = int(num_1) * int(num_2)
print("the answer is",product)
# [ ] get input of 2 numbers and display the average: (num1 + num2) divided by 2
num_1 = input("enter a number")
num_2 = input("enter a second number")
average = (int(num_1) + int(num_2)) / 2
print(average)
# [ ] get input of 2 numbers and subtract the largest from the smallest (use an if statement to see which is larger)
# show the answer
num_1 = input("enter a number")
num_2 = input("enter a second number")
if num_1<num_2:
print(int(num_1) - int(num_2))
else:
print(int(num_2) - int(num_1))
# [ ] 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
# [ ] cast the answer to an integer to cut off the decimals and print the result
num_1 = input("enter a number")
num_2 = input("enter a second number")
if num_1 == "0" or num_2 == "0":
print("0")
elif num_1<num_2:
print(int(int(num_2)/ int(num_1)))
else:
print(int(int(num_1) / int(num_2)))