return 을 안쓰면, 이렇게 result에 None이라는 값이 나옴, 왜 ?? function에 Christine이라는 value가 return 안됬으니까
Function이랑 Return 잘 쓸때까지 계속 연습 해야됨
첫번째 코드 보면 뭘 하는지 잘 모르니까, 두번째 function으로 refactoring 한 거임
Reader Understandable로 만드는게 제일 좋음 = Good naming or Comment 해야함
# 1) Complete the function to return the result of the conversion
def convert_distance(miles):
km = miles * 1.6# approximately 1.6 km in 1 mile
return km
my_trip_miles = 55
# 2) Convert my_trip_miles to kilometers by calling the function above
my_trip_km = convert_distance(my_trip_miles)
# 3) Fill in the blank to print the result of the conversion
print("The distance in kilometers is " + str(my_trip_km))
# 4) Calculate the round-trip in kilometers by doubling the result,
# and fill in the blank to print the result
print("The round-trip in kilometers is " + str(my_trip_km * 2))
deflucky_number(name):
number = len(name) * 9
string = "Hello" + name + ". Your lucky number is " + str(number)
returnstring
print(lucky_number("Wayne"))
print(lucky_number("Jia"))
def order_numbers(number1, number2):
if number2 > number1:
return number1, number2
else:
return number2, number1
(smaller, bigger) = order_numbers(100, 99)
print(smaller, bigger)
As you can see in the end we are printing (smaller,bigger) but its not defined anywhere else in the program. This gives the idea that, it must filled those blank spaces.
Multiple 하게 return 할 수 있는거 보여주는 거.
Etiher true or falsePython doesnt know how to check between number and string smaller or greaterThis case, Python totally know int(1)and str(1) is difand == 둘다 결과값 만족 해야 실행둘중에 하나만 맞아도 oktrue == false , false == true 로 만들어줌
ex) print(not 42 == "Answer")
True
execution sequence!
defhint_username(username):
iflen(username) < 3:
print("Invalid username. Must be at least 3 ch")
username letter 3개
otherwise, it skips
if 문 다음에 : 콜론 와야됨, 아니면 intended 한 error (들쑥날쑥, 아무때나 imply 되있는거) 남
Modulus = %
defis_even(number):
ifnumber % 2 == 0:
returnTrue
returnFalse
is_even(2)
이런 코드 있을때, 참고로 (% == Modulus , 나머지 값 보여주는거 , // == 몫 , ** == 제곱) if state 가 조건을 충족하면 True 로 가서 함수가 종료되고, 만약 if statement 가 충족이 안되면 다음, return False 로 가서 False 를 출력 하게 된다. Else 문 써도 되긴 하는데, 상황에 따라 다르므로 둘다 알 고 있어야 함
Odd == 홀수 , Even == 짝수
Elif Statement (else if)
defhint_username(username):
iflen(username) < 3:
print("Invalid username. Must be at least 3 ch")
eliflen(username) > 15:
print("It is way too long!")
else:
print("Valid username")
hint_username("Wayne1231242351235")
if, elif, else
Comparison operators
a == b: a is equal to b
a != b: a is different than b
a < b: a is smaller than b
a <= b: a is smaller or equal to b
a > b: a is bigger than b
a >= b: a is bigger or equal to b
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = filesize // 4096
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = filesize%4096
# Depending on whether there's a remainder or not, return
# the total number of bytes required to allocate enough blocks
partial_block = 남은 나머지 값이 있으면 더블 해야되니까 나머지값 filesize값에 blocksize값 % comparison으로 구하기
Question 6
Complete the body of the format_name function. This function receives the first_name and last_name parameters and then returns a properly formatted string.