Day 010
Udemy Python Bootcamp Day 010
Function with outputs
def my_function():
result = 3 * 2
return result #output is the result
title()
Return a version of the string where each word is titlecased.More specifically, words start with uppercased characters and all remaining cased characters have lower case.
def format_name(f_name, l_name):
f_f_name = f_name.title()
f_l_name = l_name.title()
return f"{f_f_name} {f_l_name}"
print(format_name("awESOMeE", "kIM"))
#output
Awesomee Kim
return
tells the compiter that this is the end of the function.
def format_name(f_name, l_name):
"""Take a first and last name and format it
to return the title case version of the name."""
#Docstrings
"""Docstrings are basically a way for us
to create little bits of documentation
as we're coding along in our functions
or in our other blocks of code."""
if f_name == "" or l_name == "":
return "You didn't provide valid inputs."
formated_f_name = f_name.title()
formated_l_name = l_name.title()
return f"Result: {formated_f_name} {formated_l_name}"
#Storing output in a variable
print(format_name(input("What is your first name? "), input("What is your last name? ")))
Days in Month
def is_leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
def days_in_month(year, month):
if month > 12 or month < 1:
return "Invaild month"
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap(year) and month == 2: #”2”아니라 그냥 2
#is_leap(year) == True 가 아니라 is_leap(year)자체로 사용됨 <-이것때문에 코딩룸 터미널 에러로 미리 체크 못해서 attempt 5까지 감.......ㅅㅂ
return 29
else:
return month_days[month - 1]
year = int(input("Enter a year: "))
month = int(input("Enter a month: "))
days = days_in_month(year, month)
print(days)
Calculator
from art import logo
print(logo)
#Calculator
#Add
def add(n1, n2):
return n1 + n2
#Subtract
def subtract(n1, n2):
return n1 - n2
#Multifly
def multifly(n1, n2):
return n1 * n2
#Divide
def divide(n1, n2):
return n1 / n2
operations = {
"+": add,
"-": subtract,
"*": multifly,
"/": divide,
}
num1 = int(input("What's the first number?: "))
for symbol in operations:
print(symbol)
operation_symbol = input("Pick an operation from the line above: ")
num2 = int(input("What's the second number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
Calculator2.0
def calculator():
num1 = int(input("What's the first number?: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
operation_symbol = input("Pick an operation: ")
num2 = int(input("What's the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation.: ") == "y":
num1 = answer
else:
should_continue = False
calculator()
#careful to use function in function. because without contion, it will make a infinite loop.
calculator()
Calculator - Final
from replit import clear
from art import logo
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1 / n2
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
def calculator():
print(logo)
num1 = float(input("What's the first number?: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
operation_symbol = input("Pick an operation: ")
num2 = float(input("What's the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: ") == 'y':
num1 = answer
else:
should_continue = False
clear()
calculator()
calculator()
Author And Source
이 문제에 관하여(Day 010), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@awesomee/Day-010저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)