์ƒˆ์†Œ์‹

์ธ๊ธฐ ๊ฒ€์ƒ‰์–ด

๐Ÿ“„ Python

8. ํ•จ์ˆ˜

  • -
๋ฐ˜์‘ํ˜•
# ํ•จ์ˆ˜
# ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜์™€ ์‚ฌ์šฉ์ž๊ฐ€ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค
# ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๊ฐ„์„ ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•ด ์ฝ”๋“œ ์ค„์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์ปคํ”ผ ์žํŒ๊ธฐ ํ”„๋กœ์‹œ์ €
##### ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ #####

coffee = 0

conffee= int(input("1:๋ณดํ†ต    2:์„คํƒ•    3:๋ธ”๋ž™"))
print()

print("1. ๋œจ๊ฑฐ์šด ๋ฌผ์„ ์ค€๋น„ํ•œ๋‹ค.")
print("2. ์ข…์ด์ปต์„ ์ค€๋น„ํ•œ๋‹ค.")

if coffee==1:
    print("3. ๋ณดํ†ต ์ปคํ”ผ๋ฅผ ํƒ„๋‹ค.")
elif coffee==2:
    print("3. ์„คํƒ• ์ปคํ”ผ๋ฅผ ํƒ„๋‹ค.")
elif coffee==3:
    print("3. ๋ธ”๋ž™ ์ปคํ”ผ๋ฅผ ํƒ„๋‹ค.")

print("4. ๋ฌผ์„ ๋ถ“๋Š”๋‹ค")
print("5. ์Šคํ‘ผ์œผ๋กœ ์ €์–ด์„œ ๋…น์ธ๋‹ค")
print()
print("์ปคํ”ผ๋‚˜์™”์Šต๋‹ˆ๋‹ค")

##### ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ์ปคํ”ผ ์žํŒ๊ธฐ ํ”„๋กœ์‹œ์ € #####
# ๋ณ€์ˆ˜ ์„ ์–ธ
coffee = 0

## ํ•จ์ˆ˜ ์ •์˜ ๋ถ€๋ถ„
def coffee_machine(button):
    print("1. ๋œจ๊ฑฐ์šด ๋ฌผ์„ ์ค€๋น„ํ•œ๋‹ค.")
    print("2. ์ข…์ด์ปต์„ ์ค€๋น„ํ•œ๋‹ค.")

    if button == 1:
        print("3. ๋ณดํ†ต ์ปคํ”ผ๋ฅผ ํƒ„๋‹ค.")
    elif button == 2:
        print("3. ์„คํƒ• ์ปคํ”ผ๋ฅผ ํƒ„๋‹ค.")
    elif button == 3:
        print("3. ๋ธ”๋ž™ ์ปคํ”ผ๋ฅผ ํƒ„๋‹ค.")

    print("4. ๋ฌผ์„ ๋ถ“๋Š”๋‹ค")
    print("5. ์Šคํ‘ผ์œผ๋กœ ์ €์–ด์„œ ๋…น์ธ๋‹ค")
    print()


## ๋ฉ”์ธ ์ฝ”๋“œ ๋ถ€๋ถ„
## ์ •์˜ ํ•ด ๋†“์€ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•œ๋‹ค

coffee = int(input("1:๋ณดํ†ต    2:์„คํƒ•    3:๋ธ”๋ž™"))
coffee_machine(coffee)
print("์ปคํ”ผ๋‚˜์™”์Šต๋‹ˆ๋‹ค")

coffee2 = int(input("1:๋ณดํ†ต    2:์„คํƒ•    3:๋ธ”๋ž™"))
coffee_machine(coffee2)
print("์ปคํ”ผ๋‚˜์™”์Šต๋‹ˆ๋‹ค")

coffee3 = int(input("1:๋ณดํ†ต    2:์„คํƒ•    3:๋ธ”๋ž™"))
coffee_machine(coffee3)
print("์ปคํ”ผ๋‚˜์™”์Šต๋‹ˆ๋‹ค")

# Quiz)
# ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ผ๋–ผ, ์นดํ‘ธ์น˜๋…ธ, ์นด๋ผ๋ฉœ๋งˆ๋ผ์•„๋˜ , ์—์Šคํ”„๋ ˆ์†Œ => ๋ฉ”๋‰ด๋ช… ๋ณ€๊ฒฝ
# ์นด๋ฆฌ๋‚˜, ์ง€์ ค, ๋‹๋‹, ์œˆํ„ฐ
# "์นด๋ฆฌ๋‚˜" ๊ณ ๊ฐ๋‹˜์ด ์ฃผ๋ฌธํ•˜์‹  "์นดํ‘ธ์น˜๋…ธ" ๋‚˜์™”์Šต๋‹ˆ๋‹ค.

coffee = 0
def coffee_machine(button):
    if button==1 :
        print('"'+asp+'"'+" ๊ณ ๊ฐ๋‹˜์ด ์ฃผ๋ฌธํ•˜์‹ "+' "์•„๋ฉ”๋ฆฌ์นด๋…ธ" ๋‚˜์™”์Šต๋‹ˆ๋‹ค.')
    elif button==2 :
        print('"'+asp+'"'+" ๊ณ ๊ฐ๋‹˜์ด ์ฃผ๋ฌธํ•˜์‹ "+' "์นดํŽ˜๋ผ๋–ผ" ๋‚˜์™”์Šต๋‹ˆ๋‹ค.')
    elif button==3 :
        print('"'+asp+'"'+" ๊ณ ๊ฐ๋‹˜์ด ์ฃผ๋ฌธํ•˜์‹ "+' "์นดํ‘ธ์น˜๋…ธ" ๋‚˜์™”์Šต๋‹ˆ๋‹ค.')
    elif button==4 :
        print('"'+asp+'"'+" ๊ณ ๊ฐ๋‹˜์ด ์ฃผ๋ฌธํ•˜์‹ "+' "์นด๋ผ๋ฉœ๋งˆ๋ผ์•„๋˜" ๋‚˜์™”์Šต๋‹ˆ๋‹ค.')
    elif button==5 :
        print('"'+asp+'"'+" ๊ณ ๊ฐ๋‹˜์ด ์ฃผ๋ฌธํ•˜์‹ "+' "์—์Šคํ”„๋ ˆ์†Œ" ๋‚˜์™”์Šต๋‹ˆ๋‹ค.')

coffee = int(input("1:์•„๋ฉ”๋ฆฌ์นด๋…ธ    2:์นดํŽ˜๋ผ๋–ผ    3:์นดํ‘ธ์น˜๋…ธ    4:์นด๋ผ๋ฉœ๋งˆ๋ผ์•„๋˜    5:์—์Šคํ”„๋ ˆ์†Œ"))
asp=str(input("์ฃผ๋ฌธํ•˜์‹  ๋ถ„์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : "))
coffee_machine(coffee)


## ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ๊ธฐ
# ๋ณ€์ˆ˜ ์„ ์–ธ
sum = 0
# ํ•จ์ˆ˜ ์ •์˜
def plus(v1, v2):
    result=0
    # ํ•จ์ˆ˜๋Š” ๋ฐ˜๋ณต ์ž‘์—…์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
    result = v1 + v2
    return  result

# ๋ฉ”์ธ ์ฝ”๋“œ
sum = plus(100,200)
print("%d" %sum)

print(plus(100,200))




##### ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ #####
# ํ•จ์ˆ˜ ์„ ์–ธ
def calc(v1,v2,op) :
    result= 0   # ํ˜น์‹œ ๋ชฐ๋ผ์„œ
    if op=="+":
        result=v1+v2
    elif op=="-":
        result=v1-v2
    elif op=="*":
        result=v1*v2
    elif op=="/":
        result=v1/v2
    else:
        print("์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.")

    return result
# ์ „์—ญ ๋ณ€์ˆ˜
res=0
var1,var2,oper=0,0,''
# ๋ฉ”์ธ ์ฝ”๋“œ ๋ถ€๋ถ„
var1 = int(input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))
oper = input("์—ฐ์‚ฐ์ž(+,-,*,/) ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
var2 = int(input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))

res=calc(var1,var2,oper)
print("%d %s %d = %d" % (var1,oper,var2,res))

# Quiz) ์œ„์— ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ ์ด์šฉ
# ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ฒŒ
# q ๋ˆŒ๋Ÿฌ์„œ ์ข…๋ฃŒ

def calc(v1,v2,op) :
    while True :
        var1 = input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
        oper = input("์—ฐ์‚ฐ์ž(+,-,*,/) ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
        var2 = input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")

        if var1 or var2 or oper == "q" :
            print("์ข…๋ฃŒ")
            break

        result= 0 # ํ˜น์‹œ ๋ชฐ๋ผ์„œ
        if op=="+":
            result=int(v1)+int(v2)
        elif op=="-":
            result=int(v1)-int(v2)
        elif op=="*":
            result=int(v1)*int(v2)
        elif op=="/":
            result=int(v1)/int(v2)
        else:
            print("์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.")

        return result

# ์ „์—ญ ๋ณ€์ˆ˜
res=0
res=calc(var1,var2,oper)
print(var1,oper,var2,res)





def calc(v1,v2,op) :
    while True :
        v1 = input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
        op = input("์—ฐ์‚ฐ์ž(+,-,*,/) ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
        v2 = input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")

        if var1 or var2 or oper == "q" :
            print("์ข…๋ฃŒ")
            break
    result= 0   # ํ˜น์‹œ ๋ชฐ๋ผ์„œ
    if op=="+":
        result=v1+v2
    elif op=="-":
        result=v1-v2
    elif op=="*":
        result=v1*v2
    elif op=="/":
        result=v1/v2
    else:
        print("์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.")

    return result
# ์ „์—ญ ๋ณ€์ˆ˜
res=0
var1,var2,oper=0,0,''
res=calc(var1,var2,oper)
print(var1,oper,var2," = ",res)


### ์ง€์—ญ๋ณ€์ˆ˜ (๋งค๊ฐœ๋ณ€์ˆ˜, ํŒŒ๋ผ๋ฏธํ„ฐ), ์ „์—ญ๋ณ€์ˆ˜

def func1():
    a = 10
    print(("func1์—์„œ์˜ a๊ฐ’ : %d") %a)

def func2():
    print(("func2์—์„œ์˜ a๊ฐ’ : %d") % a)

func1()
func2()

## ๊ธ€๋กœ๋ฒŒ ์˜ˆ์•ฝ์–ด ( ์ „์—ญ๋ณ€์ˆ˜ )


def func3():
    global v1 # ์ง€์—ญ๋ณ€์ˆ˜ -> ์ „์—ญ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„๋•Œ
    v1 = 100
    print(("func1์—์„œ์˜ v1๊ฐ’ : %d") %v1)

def func4():
    print(("func2์—์„œ์˜ v1๊ฐ’ : %d") %v1)

func3()
print(v1)

func4()

## ํ•จ์ˆ˜์˜ ์ค‘์š”์š”์†Œ 2๊ฐ€์ง€
# ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์ˆ˜์ ์ด์ง„ ์•Š์Œ
# return๊ฐ’์ด ํ•„์ˆ˜์ ์ด์ง„ ์•Š์Œ


# return ๊ฐ’์ด ์žˆ๊ณ  ์—†๊ณ  ์ฐจ์ด
def func1():
    result = 100
    return result
def func2():
    print("๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜")

sum = func1()
print("func1์˜ ๋ฐ˜ํ™˜๊ฐ’ : %d" %sum)

func2()
# ํ”„๋ฆฐํŠธ ๊ตฌ๋ฌธ ์‹คํ–‰

# return ๊ฐ’์ด ์—ฌ๊ฑฐ๋ž˜์ธ ํ•จ์ˆ˜
def multi(v1,v2):
    result = []
    res1=v1+v2
    res2=v1-v2
    result.append(res1)
    result.append(res2)
    return result
ll=[]
sum,min=0,0

ll=multi(300,200)
sum=ll[0]
min = ll[1]
print(sum,min)

### ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์„ ๋•Œ ๋””ํดํŠธ ๊ฐ’ ์„ค์ •
def para_func(v1,v2,v3=0):
    result=0
    result= v1+v2+v3
    return result

# print(para_func(20,30,40))
print(para_func(20,30)) # ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆซ์ž๊ฐ€ ๋งž์ง€ ์•Š์€ ์—๋Ÿฌ, v3=0๋””ํดํŠธ๊ฐ’ ์ง€์ •


## ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
def para_func(*para): # : ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๋ฉด ๊ฐฏ์ˆ˜ ์ œํ•œ x
    result = 0
    for num in para:
        result=result+num
    return result

sum = 0
sum = para_func(10,20)
print(sum)

sum = para_func(10,20,30,40,50)
print(sum)


## ์žฌ๊ท€ํ•จ์ˆ˜
def selfcall():
    print("ํ˜œํ˜œ", end='')
    selfcall()

selfcall()





## Quiz) ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž…๋ ฅ ๋ฐ›์•„ ๋ฐ›์€๊ฑฐ ์ „๋ถ€ ๋‹ค ๋”ํ•˜๊ธฐ

def para_func(*para): # : ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๋ฉด ๊ฐฏ์ˆ˜ ์ œํ•œ x
    result = 0
    for num in para:
        result=result+num
    return result

sum = 0
while True :
    sum = para_func(int(input("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”")))
    if sum =='0':
        break
        print("์ข…๋ฃŒ")



## pass ์˜ˆ์•ฝ์–ด
# ์•„๋ฌด๊ฒƒ๋„ ์—†์„๋•Œ ์Šคํ‚ต

def func7():
    if True:
        pass
    else:
        print("else ๊ตฌ๋ฌธ ์ž…๋‹ˆ๋‹ค.")


## ๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ๊ธฐ
# ํŒจํ‚ค์ง€(๋ชจ๋“ˆ) > ํ•จ์ˆ˜๊ฐ€ ๋ชจ์—ฌ์žˆ๋Š” ํŒŒ์ผ
# random ํŒจํ‚ค์ง€ > randrange ํ•จ์ˆ˜

import random

def getNumber():
    return random.randrange(1,46)

lotto=[]
num=0
print("##### ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ถ”์ฒจํ•ฉ๋‹ˆ๋‹ค ##### \n")

while True:
    num = getNumber()
    if lotto.count(num)==0:
        lotto.append(num)
    if len(lotto)>=6:
        break

print("์ถ”์ฒจ ๋กœ๋˜ ๋ฒˆํ˜ธ : ",end='')
lotto.sort()
for i in range(0,6):
    print("%d " %lotto[i],end='')


# Quiz) ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ถ”์ฒจ

import random

def getNumber():
    return random.randrange(1,46)

lotto=[]
num=0
bonus=0
print("##### ๋กœ๋˜ ๋ฒˆํ˜ธ ์ถ”์ฒจ ##### \n")

while True:
    num = getNumber()
    bonus= getNumber()
    if lotto.count(num)==0:
        lotto.append(num)
    if len(lotto)>=6:
        break
    if lotto.count(bonus)==0:
        lotto.append(bonus)
    if len(lotto)==1:
        break

print("์ถ”์ฒจ ๋กœ๋˜ ๋ฒˆํ˜ธ : ",end='')
lotto.sort()
for i in range(0,6):
    print("%d " %lotto[i],end='')

print("\n์ถ”์ฒจ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ : %d" %bonus ,end='')

# Quiz) ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ถ”์ฒจ

import random

def getNumber():
    return random.randrange(1,46)

lotto=[]
num=0
print("##### ๋กœ๋˜ ๋ฒˆํ˜ธ ์ถ”์ฒจ ##### \n")

while True:
    num = getNumber()
    if lotto.count(num)==0:
        lotto.append(num)
    if len(lotto)>=7:
        break

print("์ถ”์ฒจ ๋กœ๋˜ ๋ฒˆํ˜ธ : ",end='')

ll6=lotto[0:6]
ll6.sort()
print(ll6)

print("์ถ”์ฒจ ๋กœ๋˜ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ : ",end='')
print(lotto[6])
๋ฐ˜์‘ํ˜•

'๐Ÿ“„ Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

10. ํŒŒ์ผ ์ž…์ถœ๋ ฅ  (0) 2022.06.09
9. ๋ชจ๋“ˆ  (0) 2022.06.09
7. ๋ฌธ์ž์—ด  (0) 2022.06.09
6. ๋ฆฌ์ŠคํŠธ ํŠœํ”Œ ๋”•์…”๋„ˆ๋ฆฌ  (0) 2022.06.09
5. while ๋ฐ˜๋ณต๋ฌธ  (0) 2022.06.09
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.