본문 바로가기
솔루션모음/파워 유저를 위한 파이썬 express 실습문제

[파워 유저를 위한 파이썬express] 6장 프로그래밍 programming 솔루션 답지

by 이얏호이야호 2023. 1. 8.

1.
n = int(input("입력할 값의 개수: "))
result = [ ]

for i in range(n):
    value = int(input(""))
    result.append(value)

s = sum(result)
print("값의 합계=", s)



3.
numbers = [20, 1, 12, 9, 18]

for value in   numbers:
    print(value, "\t", "*" *   value)


5.
def match_words(words):
  ctr = 0

  for word in words:
    if len(word) > 1 and word[0] == word[-1]:
      ctr += 1
  return ctr


s= ['aba', 'xyz', 'abc', '121']
print(s)
print('문자열의 개수=', match_words(s))

7.
import random
list1 = ["a"+str(i) for i in range(10)]
print("list1=", list1)
print(random.choice(list1))

9.
import turtle

def draw_thing(some_turtle, tasks):
    for value in tasks:
        some_turtle.forward(value)
        some_turtle.stamp()
        some_turtle.backward(value)
        some_turtle.right(30)

window = turtle.Screen()
bob = turtle.Turtle()

my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
draw_thing(bob, my_list)
turtle.mainloop()
turtle.bye()

11.
import turtle
import random
player_one = turtle.Turtle()
player_one.color("blue")
player_one.shape("arrow")
player_one.penup()
player_one.goto(-200,100)
player_two = player_one.clone()
player_two.color("red")
player_two.shape("turtle")
player_two.penup()
player_two.goto(-200,-100)

player_one.goto(300,60)
player_one.pendown()
player_one.circle(40)
player_one.penup()
player_one.goto(-200,100)
player_two.goto(300,-140)
player_two.pendown()
player_two.circle(40)
player_two.penup()
player_two.goto(-200,-100)

player_one.pendown()
player_two.pendown()
die = [1,2,3,4,5,6]
for i in range(20):
    if player_one.pos() >= (300,100):
            print("첫 번째 플레이어가 이겼습니다. ")
            break
    elif player_two.pos() >= (300,-100):
            print("두 번째 플레이어가 이겼습니다. ")
            break
    else:
            player_one_turn = input("주사위를 던지려면 엔터키를 누르세요. ")
            die_outcome = random.choice(die)
            print("주사위 결과: ")
            print(die_outcome)
            print("진행 위치: ")
            print(20*die_outcome)
            player_one.fd(20*die_outcome)
            player_two_turn = input("주사위를 던지려면 엔터키를 누르세요. ")
            d = random.choice(die)
            print("주사위 결과: ")
            print(d)
            print("진행 위치: ")
            print(20*d)
            player_two.fd(20*d)
            
turtle.mainloop()
turtle.bye()


13.
seats = []
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])
seats.append([0,0,0,0,0,0,0,0,0,0])

def displayBookings():
  print("======================================")
  print("0 1 2 3 4 5 6 7 8 9 10")
  print("======================================")
  for row in seats:
    print(row)
  print("")

def reserv():
  row =     int(input("원하시는 좌석의 행번호를 입력하세요(종료는 –1)"))
  column = int(input("원하시는 좌석의 열번호를 입력하세요(종료는 –1)"))
  
  if seats[row][column]==1:
    print("이미 예약된 자리입니다.")
  else:
    print("예약합니다.")
    seats[row][column]=1

displayBookings()
reserv()  
displayBookings()

15.
import random

def longestrun(myList):
    size = 1
    max_size = 0
    for i in range(len(myList)-1):
        if myList[i+1] == myList[i]:
            size += 1
        else: 
            size = 1
        if max_size<size:
            max_size = size
    return max_size 

s = [ random.randint(0, 1) for _ in range(10) ]
print(s)
print("최대 연속 길이=", longestrun(s))

 

댓글