Day 10 – Functions with Outputs

  • Day 10 Goals
  • Functions with Outputs (return)
  • Multiple return values
  • Coding Exercise: leap year
  • Docstrings
  • Project: Calculator

Coding Exercise:

def is_leap_year(year):
    leap_year = True
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                leap_year = True
                return leap_year
            else:
                leap_year = False
                return leap_year
        else:
            leap_year = False
            return leap_year
    else:
        leap_year = False
    return leap_year


print(is_leap_year(1989))

Project Code – This is my current code but the error handling needs to be improved.. It has the basic functionality as long as the users doesn’t make a mistake with their typing

def calculator(f_num, op, s_num):
    if op == "+":
        answer=float(f_num) + float(s_num)
    elif op == "-":
        answer=float(f_num) - float(s_num)
    elif op == "*":
        answer = float(f_num) * float(s_num)
    elif op == "/":
        answer = float(f_num) / float(s_num)
    else:
        print("Invalid operator. Please try again.")
        return
    return answer

returned_answer = calculator(input("What's the first number? "), input("Pick an operator: \n+\n-\n*\n/ "), input("What's the second number? "))

response = input(f"Would you like to continue with the answer 'yes' or 'no'  {returned_answer}: ")

while response == "yes".lower():
    returned_answer = calculator(returned_answer, input("what operation would you like: \n+\n-\n*\n/ "), input("What's the next number? "))
    print(returned_answer)
    response = input(f"Would you like to continue with the answer {returned_answer} type 'yes':")
    if response != "yes".lower():
        print("Invalid input. Please try again.")

print("Thank you for choosing Calculator")