본문 바로가기
프로그램/Python 1000제

Python 223제] 두근두근 파이썬(개정판) CHAPTER 5 도전문제

by 건티 2023. 11. 20.
728x90

출처 : 반크_반크 20년 백서

 

p136

참고풀이]

import turtle
t = turtle.Turtle()
t.shape("turtle")

t.penup() #펜을 올려서 그림이 그려지지 않게 한다. 
t.goto(120, 100) #거북이를 (120, 100)으로 이동시킨다. 
t.write("거북이가 여기로 오면 양수입니다.")
t.goto(120, 0) #거북이를 (120, 0)으로 이동시킨다.
t.write("거북이가 여기로 오면 0입니다.")
t.goto(120, -100) #거북이를 (120, -100)으로 이동시킨다.
t.write("거북이가 여기로 오면 음수입니다.")

t.goto(0, 0) #(0, 0) 위치로 거북이를 이동시킨다.
t.pendown() #펜을 내려서 그림이 그려지게 한다. 
s = turtle.textinput("", "숫자를 입력하시오: ")
n=int(s)
if n > 0 :
    t.color("blue")
    t.goto(100, 100)
if n == 0 :
    t.goto(100, 0)
if n < 0 :
    t.color("red")
    t.goto(100, -100)
turtle.done()

 

참고풀이 결과]

초기화면)

 

양수를 입력하면)

 

0을 입력하면)

 

음수를 입력하면)

 

 

p138

참고풀이]

print("안녕하세요? 두근두근 영화관입니다.")
age = int(input("나이를 입력하시오 : "))

if age >= 15:
    print("\n본 영화를 보실 수 있습니다.")
    print("영화의 가격은 10000원입니다.")
else:
    print("\n본 영화를 보실 수 없습니다.")
    print("다른 영화를 보시겠어요?")

 

참고풀이 결과]

 

 

 

p141

참고풀이]

import turtle

# 거북이를 만든다.
t = turtle.Turtle()

# 커서의 모양을 거북이로 한다. 
t.shape("turtle")

# 거북이가 그리는 선의 두께를 3으로 한다. 
t.width(3)

# 거북이를 3배 확대한다. 
t.shapesize(3, 3)

# 무한 루프이다. 
while True:
    command = input("명령을 입력하시오: ")
    #사용자가 “l“ 또는 "left"를 입력하였으면
    if command == "l" or command=="left":
        t.left(90)
        t.forward(100)
    #사용자가 “r“ 또는"right"를 입력하였으면
    if command == "r" or command=="right":
        t.right(90)
        t.forward(100)
    #사용자가 "quit"를 입력하였으면
    if command=="quit":
        turtle.bye()
        exit() #sys.exit()
turtle.done()

 

참고풀이 결과]

 

 

 

 

p143

참고풀이]

import random

print("주사위 던지기 게임을 시작합니다.\n")
dice = random.randrange(6)
if dice == 0 :
print("6입니다.")
if dice == 1 :
print("1입니다.")
if dice == 2 :
print("2입니다.")
if dice == 3 :
print("3입니다.")
if dice == 4 :
print("4입니다.")
if dice == 5 :
print("5입니다.")

print("게임이 종료되었습니다.")

 

참고풀이 결과]

 

 

p146

참고풀이]

import random

time = random.randint(1, 24)
print("지금 시각은 " + str(time) + "시 입니다.")
sunny = random.choice([True, False])
if sunny:
print ("현재 날씨가 화창합니다. ")
else:
print ("현재 날씨가 화창하지 않습니다. ")

# 종달새가 노래를 할 것인지를 판단해보자. 
if (time >= 6 and time < 9 or time >= 14 and time < 16) and sunny:
print ("종달새가 노래를 합니다.")
else:
print ("종달새가 노래를 하지 않습니다.")

 

참고풀이 결과]

 

 

 

p148

참고풀이]

Id = "ilovepython"
Password="123456"

s = input("아이디를 입력하시오 : ")
pw= input("패스워드를 입력하시오 : ")

if s == Id and pw==Password:
    print("환영합니다.")
else:
    if s==Id and pw != Password:
        print("패스워드를 찾을 수 없습니다.")
    if s != Id and pw == Password:
        print("아이디를 찾을 수 없습니다.")
    if s != Id and pw != Password:
        print("아이디와 패스워드를 찾을 수 없습니다.")

 

참고풀이 결과]

 

 

p149

참고풀이]

import os

command = input("명령을 입력하시오: ")

if command == "shutdown":
    print("컴퓨터가 곧 종료됩니다. 엔터키를 누르세요")
    input()
    os.system("shutdown /s /t 1")
elif command == "cwd":
    Cwd = os.getcwd()
    print("Current working directory :", Cwd) 
elif command == "mkdir":
    #현재 파일이 저장된 폴더에 'Mkdir' 폴더를 만든다.
    os.mkdir("Mkdir") 
else:
    print("알수 없는 명령어입니다.")

 

참고풀이 결과]

 

 

 

 

p150

참고풀이]

import random
options=["왼쪽중앙","중앙","오른쪽 중앙","왼쪽 상단",
         "왼쪽 하단","오른쪽 상단","오른쪽 하단"]
computer_choice = random.choice(options)

print("[왼쪽 상단, 왼쪽 중앙, 왼쪽 하단, 중앙, 오른쪽 상단, 오른쪽 중앙, 오른쪽 하단]")
user_choice = input("어디를 수비하시겠어요? ")
if computer_choice == user_choice:
print("수비에 성공하셨습니다. ")
else:
print("페날티킥이 성공하였습니다. ")

 

참고풀이 결과]

 

 

p152

참고풀이]

import turtle
t = turtle.Turtle()
t.shape("turtle")

s = turtle.textinput("", "도형을 입력하시오: ")
if s == "사각형" :
    w = turtle.numinput("","가로 : ")
    h = turtle.numinput("","세로 : ")
    t.forward(w)
    t.left(90)
    t.forward(h)
    t.left(90)
    t.forward(w)
    t.left(90)
    t.forward(h)

elif s == "삼각형" :
    w=turtle.numinput("","길이 : ")
    t.fd(w)
    t.left(120)
    t.fd(w)
    t.left(120)
    t.fd(w)

else : #원
    r=turtle.numinput("","반지름 : ")
    t.circle(r)


turtle.done()

 

참고풀이 결과]

 

 

 

 

 

 

대한민국의 아름다운 영토, 독도

 

 

 

 

반응형

댓글