Complete the function digits(n) that returns how many digits the number has. For example: 25 has 2 digits and 144 has 3 digits. Tip: you can figure out the digits of a number by dividing it by 10 once per digit until there are no digits left.
This function prints out a multiplication table (where each number is the result of multiplying the first number of its row by the number at the top of its column). Fill in the blanks so that calling multiplication_table(1, 3) will print out:
start 에서 stop 까지 그 값 곱한 값 보여줘
1 2 3
2 4 6
3 6 9
def multiplication_table(start, stop):
for x in ___:
for y in ___:
print(str(x*y), end=" ")
print()
multiplication_table(1, 3)
# Should print the multiplication table shown above
range function 안쓰면 int 는 iterable 못 하다고 나옴. 꼭 ragne function 써야됨
stop +1하는 이유는, stop 값을 포함 시키기 위함
answer
def multiplication_table(start, stop):
for x inrange(start,stop+1):
for y inrange(start,stop+1):
print(str(x*y), end=" ")
print()
multiplication_table(1, 3)
# Should print the multiplication table shown above
end=" " 뜻은, print 하고 마지막에 뭐 붙일지 보여 준다는 것
이중 for 문은
foriinrange(2,10):
foryinrange(2,10):
print(i,"*",y,"=",i*y)
이런식으로 위에 i 리스트 값 1개 가지고와서 y for문 끝날때까지 계속 하는거, 구구단 으로 예를 들면 편하다
1 2 3 이렇게 나오는거는, 1*1 = 1 , 1*2 = 2 , 1*3= 3 다음줄로 가서(i ==2 ) 진행 되는 방식
The counter function counts down from start to stop when start is bigger than stop, and counts up from start to stop otherwise. Fill in the blanks to make this work correctly.
숫자 비교해서, start 값이 stop보다 높으면 내림차순, 반대면 오름차순, 같으면 up 으로 하나만 나오게 출력
def counter(start, stop):
x = start
if ___:
return_string = "Counting down: "
while x >= stop:
return_string += str(x)
if ___:
return_string += ","
___
else:
return_string = "Counting up: "
while x <= stop:
return_string += str(x)
if ___:
return_string += ","
___
return return_string
print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5"
Answer
def counter(start, stop):
x = start
if x > stop:
return_string = "Counting down: "
while x > stop:
return_string += str(x)
if x >= stop:
return_string += ","
x -= 1
else:
return_string = "Counting up: "
while x < stop+1:
return_string += str(x)
if x < stop:
return_string += ","
x += 1
return return_string
print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
The even_numbers function returns a space-separated string of all positive numbers that are divisible by 2, up to and including the maximum that's passed into the function. For example, even_numbers(6) returns “2 4 6”. Fill in the blank to make this work.
def even_numbers(maximum):
return_string = ""
for x in ___:
return_string += str(x) + " "
return return_string.strip()
print(even_numbers(6)) # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1)) # No numbers displayed
print(even_numbers(3)) # Should be 2
print(even_numbers(0)) # No numbers displayed
answer
def even_numbers(maximum):
return_string = ""
for x inrange(1,maximum+1,2):
return_string += str(x) + " "
return return_string.strip()
print(even_numbers(6)) # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1)) # No numbers displayed
print(even_numbers(3)) # Should be 2
print(even_numbers(0)) # No numbers displayed
for in ragne(1,maximum+,2) 3번째 parameter 조건, +2해 주는거 1,3,5,7 이런식으로 가게하는, 이게 핵심인듯