πŸ“š μŠ€ν„°λ””/파이썬 μŠ€ν„°λ”” 과제 제좜

[4νŒ€/κΉ€μœ€] 5μ°¨μ‹œ 파이썬 과제 - ν•¨μˆ˜

μ•Œ 수 μ—†λŠ” μ‚¬μš©μž 2023. 5. 9. 00:06

1. 이름을 인수둜 λ°›μ•„ λ‹€μŒκ³Ό 같이 인삿말을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

ν™κΈΈλ™λ‹˜ μ•ˆλ…•ν•˜μ„Έμš”!

-> λ‹΅ :

name = input("name : ")
print(f"{name}λ‹˜ μ•ˆλ…•ν•˜μ„Έμš”!")

1번 문제 μ‹€ν–‰κ²°κ³Ό

 

2. λ‹€μŒ μ½”λ“œλ₯Ό μ‹€ν–‰ν–ˆμ„ λ•Œμ˜ 좜λ ₯ κ²°κ³Όλ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

def f(x):
    y = x
    x = 7
    return y * x

x = 4
print(f(3))
print(x)

-> λ‹΅ : 21, 4

2번 문제 μ‹€ν–‰κ²°κ³Ό

 

3. f(3)μ—μ„œμ˜ x와 y의 값이 무엇인지 μž‘μ„±ν•˜μ‹œμ˜€.

-> λ‹΅ : xκ°’ : 7, yκ°’: 3

 

4. μ§€μ—­λ³€μˆ˜μ™€ μ „μ—­λ³€μˆ˜ 각각 ν•˜λ‚˜μ”© μ‚¬μš©ν•˜μ—¬ λ‹€μŒκ³Ό 같이 좜λ ₯ν•˜μ‹œμ˜€.

Value : 10
Value : 20

-> λ‹΅ : 

def pValue(Value):
    print("Value :", Value)

pValue(10)
Value = 20
print("Value :", Value)

4번 문제 μ‹€ν–‰κ²°κ³Ό

 

5. λ‹€μŒ μ½”λ“œμ˜ 좜λ ₯ κ²°κ³Όλ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

a = 111
b = 222


def function_1():
    print(a)
    print(b)


def function_2():
    a = 333
    print(a)
    print(b)


function_1()
function_2()

-> λ‹΅ : 111
           222

           333

           222

5번 문제 μ‹€ν–‰κ²°κ³Ό

 

6. λ‹€μŒ 쑰건에 맞좰 에어컨 리λͺ¨μ½˜ ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“œλ €κ³  ν•œλ‹€.

  a. μ‚¬μš©μžμ—κ²Œ μ˜¨λ„λ₯Ό 18 ~ 28의 숫자둜 μž…λ ₯λ°›μ•„ 에어컨 μ˜¨λ„λ₯Ό μ„€μ •ν•˜κ³ ,

  b. λͺ©ν‘œ μ˜¨λ„κ°€ μ„€μ •λ˜λ©΄ ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄ ν˜„μž¬ μ˜¨λ„μ—μ„œ λͺ©ν‘œ μ˜¨λ„κΉŒμ§€ 1μ”© 증가, κ°μ†Œν•˜μ—¬ λͺ©ν‘œ μ˜¨λ„κ°€ λ˜λ„λ‘ ν•œλ‹€.

  c. 'μ’…λ£Œ'λ₯Ό μž…λ ₯λ°›μœΌλ©΄ ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•œλ‹€.

  d. ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•˜κΈ° μ „κΉŒμ§€λŠ” 계속 μ˜¨λ„λ₯Ό μž…λ ₯받을 수 μžˆμ–΄μ•Ό ν•œλ‹€.

  e. μ‹œμž‘ μ˜¨λ„λŠ” 21도이닀.

    cur_tem : ν˜„μž¬ μ˜¨λ„

    des_tem : λͺ©ν‘œ μ˜¨λ„

    put_tem : μž…λ ₯받은 μ˜¨λ„

    set_tem : μ˜¨λ„λ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜

cur_tem = 21 # ν˜„μž¬ μ˜¨λ„

def set_tem
...

print("에어컨 μž‘λ™μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.")

while True:
    put_tem = input("μ˜¨λ„ μ„€μ •ν•˜κΈ° : ")
    if (   (1)   ): # μ’…λ£Œλ₯Ό μž…λ ₯λ°›μœΌλ©΄ μ’…λ£Œ
        print("μž‘λ™μ„ μ€‘μ§€ν•©λ‹ˆλ‹€.")
        break
    elif(   (2)   ): # μ μ ˆν•œ μ˜¨λ„ 값을 μž…λ ₯λ°›μœΌλ©΄ set_tem ν•¨μˆ˜ 호좜
        (   (3)   ) # ν˜„μž¬ μ˜¨λ„ μ•Œλ¦¬κΈ°
        (   (4)   ) # μ˜¨λ„λ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜ ν˜ΈμΆœν•˜κΈ°
    else:
        print("μž…λ ₯을 ν™•μΈν•΄μ£Όμ„Έμš”")

whileλ¬Έκ³Ό if문을 μ‚¬μš©ν•˜μ—¬ μ‚¬μš©μžμ—κ²Œ μ˜¨λ„λ₯Ό 18 ~ 28 μ‚¬μ΄μ˜ 숫자둜 μž…λ ₯ λ°›λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•˜κΈ° μœ„ν•΄ λ‹€μŒ κ΄„ν˜Έλ₯Ό μ±„μš°μ‹œμ˜€.

-> λ‹΅ :

     (1) : put_tem == "μ’…λ£Œ"

     (2) : (int(put_tem) > 18) and (int(put_tem) < 28)

     (3) : print(f"ν˜„μž¬ μ˜¨λ„λŠ” {cur_tem}도 μž…λ‹ˆλ‹€.")

     (4) : set_tem(int(put_tem)

 

7. μ˜¨λ„λ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜ set_tem ν•¨μˆ˜λ₯Ό μ •μ˜ν•˜λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•˜κΈ° μœ„ν•΄ λ‹€μŒ κ΄„ν˜Έλ₯Ό μ±„μš°μ‹œμ˜€.

cur_tem = 21 # ν˜„μž¬ μ˜¨λ„

def set_tem(   (a)   ): # μ˜¨λ„ μ„€μ • ν•¨μˆ˜
    (   (b)   ) # ν˜„μž¬ μ˜¨λ„ λ°›μ•„μ˜€κΈ°
...

print("에어컨 μž‘λ™μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.")

while True:
    put_tem = input("μ˜¨λ„ μ„€μ •ν•˜κΈ° : ")
    if (   (1)   ): # μ’…λ£Œλ₯Ό μž…λ ₯λ°›μœΌλ©΄ μ’…λ£Œ
        print("μž‘λ™μ„ μ€‘μ§€ν•©λ‹ˆλ‹€.")
        break
    elif(   (2)   ): # μ μ ˆν•œ μ˜¨λ„ 값을 μž…λ ₯λ°›μœΌλ©΄ set_tem ν•¨μˆ˜ 호좜
        (   (3)   ) # ν˜„μž¬ μ˜¨λ„ μ•Œλ¦¬κΈ°
        (   (4)   ) # μ˜¨λ„λ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜ ν˜ΈμΆœν•˜κΈ°
    else:
        print("μž…λ ₯을 ν™•μΈν•΄μ£Όμ„Έμš”")

-> λ‹΅ : 

      (a) : des_tem

      (b) : global cur_tem

 

8. set_tem ν•¨μˆ˜μ˜ λͺ…령문을 μž‘μ„±ν•˜κ³ μž ν•œλ‹€. κ΄„ν˜Έλ₯Ό μ±„μ›Œ μ½”λ“œλ₯Ό μ™„μ„±ν•˜μ‹œμ˜€.

cur_tem = 21 # ν˜„μž¬ μ˜¨λ„

def set_tem(   (a)   ): # μ˜¨λ„ μ„€μ • ν•¨μˆ˜
    (   (b)   ) # ν˜„μž¬ μ˜¨λ„ λ°›μ•„μ˜€κΈ°
    if cur_tem < des_tem :
        while(cur_tem < des_tem):
            (   (γ„±)   )
            print(f"ν˜„μž¬ μ˜¨λ„ : {cur_tem}도")
    else:
        while(cur_tem > des_tem):
            (   (γ„΄)   )
            print(f"ν˜„μž¬ μ˜¨λ„ : {cur_tem}도")

print("에어컨 μž‘λ™μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.")

while True:
    put_tem = input("μ˜¨λ„ μ„€μ •ν•˜κΈ° : ")
    if (   (1)   ): # μ’…λ£Œλ₯Ό μž…λ ₯λ°›μœΌλ©΄ μ’…λ£Œ
        print("μž‘λ™μ„ μ€‘μ§€ν•©λ‹ˆλ‹€.")
        break
    elif(   (2)   ): # μ μ ˆν•œ μ˜¨λ„ 값을 μž…λ ₯λ°›μœΌλ©΄ set_tem ν•¨μˆ˜ 호좜
        (   (3)   ) # ν˜„μž¬ μ˜¨λ„ μ•Œλ¦¬κΈ°
        (   (4)   ) # μ˜¨λ„λ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜ ν˜ΈμΆœν•˜κΈ°
    else:
        print("μž…λ ₯을 ν™•μΈν•΄μ£Όμ„Έμš”")

-> λ‹΅ :

     (γ„±) : cur_tem += 1

     (γ„΄) : cur_tem -= 1

6번 ~ 8번 μ‹€ν–‰ κ²°κ³Ό

 

9. 두 개의 μ •μˆ˜λ₯Ό μž…λ ₯ λ°›μ•„μ„œ λ§μ…ˆ, λΊ„μ…ˆ, κ³±μ…ˆ, λ‚˜λˆ—μ…ˆμ„ ν•˜λŠ” 계산기 ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄ λ‹€μŒκ³Ό 같이 좜λ ₯ν•˜μ‹œμ˜€.

계산을 μž…λ ₯ν•˜μ„Έμš” (+, -, *, / 쀑 ν•˜λ‚˜): -
첫 번째 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” : 45
두 번째 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” : 12
계산 κ²°κ³Ό: 33

-> λ‹΅ :

def cal():
    mode = input("계산을 μž…λ ₯ν•˜μ„Έμš” (+, -, *, / 쀑 ν•˜λ‚˜): ") 
    a = int(input("첫 번째 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” :"))
    b = int(input("두 번째 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” :"))
    if mode == "+":
        result = a + b
    elif mode == "-":
       result = a - b
    elif mode == "*":
       result = a * b
    elif mode == "/":
      result = a / b
    else:
       print("μ˜¬λ°”λ₯Έ 기호λ₯Ό μ„ νƒν•΄μ£Όμ„Έμš”")
    return result

print(cal())

9번 문제 μ‹€ν–‰κ²°κ³Ό