Language/Python

두근두근 파이썬 CH4 연습문제

Return 2021. 6. 22. 19:25

1번.

print("나는 "+str(12)+"개의 사과를 먹었다.") #문자열과 숫자는 합칠수 없다 숫자 12를 string으로 감싸줘야된다.
print("나는",12,"개의 사과를 먹었다.")# +대신 ,를 써도 된다.

2번.

print("apple" + "grape")  # applegrape로 출력된다.
print("apple" * 3)  #appleappleapple로 출력된다.

3번.

s = input("문자열을 입력하세요 : ")

print(s[0:2]+s[-2:])  # s[0:2]를 사용하면 1,2번째 문자를 출력할수있다. 
                      # s[-2:]를 사용하면 마지막 2글자를 출력할수있다.

4번.

s= input("문자열을 입력하시오 : ")  #문자열을 input으로 받아온다. 

print(s+"하는 중")  #바다온 문자열에 +를 이용해 "하는중"을 붙인다.

5번.

a = input("기호르 입력하시오 ") #기호를 받아온다. 
s = input("중간에 삽입할 문자열을 입력하시오 ") #문자열을 받아온다.

print(a[0]+s+a[1]) #받아온 기호의 첫번째와 마지막번째 사이에 받아온 문자열을 삽입한다.

6번.

list_num = [1,2,3,4] #리스트를 만든다.

list_sum = list_num[0]+list_num[1]+list_num[2]+list_num[3] # 리스트의 1,2,3,4 번째 숫자를 더한다.

print("리스트 숫자들의 합 : {}".format(list_sum)) #format 문을 이용해 print문에 넣는다.

7번.

import turtle as t #turtle import

color_list = [] #컬러 리스트를 만든다.
a = input("색상 #1을 입력하시오 :")  #원하는 색상을 입력받는다.
color_list.append(a)  #리스트에 append한다.
b = input("색상 #2을 입력하시오 :")
color_list.append(b)
c = input("색상 #3을 입력하시오 :")
color_list.append(c)

t.shape("turtle") #모양은 turtle

t.fillcolor(color_list[0]) #fillcolor를 이용해 원하는 색상을 지정한다.
t.begin_fill() #채우기 시작한다.
t.circle(50) #원을 그린다.
t.end_fill() # 채우기 종료
t.up() #팬을 위로 든다.
t.goto(100,0) #해당좌표로 이동한다.
t.down() #팬을 다시 내려 놓는다.

t.fillcolor(color_list[1]) # 위와 동일
t.begin_fill()
t.circle(50)
t.end_fill()
t.up()
t.goto(200,0)
t.down()

t.fillcolor(color_list[2]) #위와 동일
t.begin_fill()
t.circle(50)
t.end_fill()

t.exitonclick() #터틀이 종료되지 않도록 한다.

8번.

import turtle as t

t.shape("turtle")

coordinate_list = [] #리스트를 만든다.

a = int(input("x1:"))  #x1을 받아온다.
coordinate_list.append(a) #리스트에 append한다.
b = int(input("y1:"))
coordinate_list.append(b)
c = int(input("x2:"))      #위와 동일
coordinate_list.append(c)
d = int(input("y2:"))      #위와 동일
coordinate_list.append(d)
e = int(input("x3:"))      #위와 동일
coordinate_list.append(e)
f = int(input("y3:"))      #위와 동일
coordinate_list.append(f)

t.goto(coordinate_list[0],coordinate_list[1]) #해당좌표로 이동한다. 0번째부터 시작한다는것에 주의!
t.goto(coordinate_list[2],coordinate_list[3])
t.goto(coordinate_list[4],coordinate_list[5])

t.exitonclick() #터틀이 종료되지 않도록 한다.