본문 바로가기
WEB/Python-Django

Python 코루틴

by DeveloperCat 2025. 5. 31.
반응형
##    result = False
##    while True:
##        line = (yield result)   # result 값을 외부로 보내고, send로 다음 문자열을 line 변수로 받음
##        result = word in line   # line 안에 word가 있는지 검사
##
##f = find('Python')  # Python이라는 단어를 찾는 제너레이터 생성
##next(f) # 제너레이터를 초기화하고 첫 yield까지 실행(result=false)
## 
##print(f.send('Hello, Python!'))
##print(f.send('Hello, world!'))
##print(f.send('Python Script'))
## 
##f.close()   # 제너레이터 함수 종료 메서드

## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

def calc():
    result = 0
    while True:
        line = (yield result)
        words = line.split()
        if words[1] == '+':
            result = int(words[0])+int(words[2])
        elif words[1] == '-':
            result = int(words[0])-int(words[2])
        elif words[1] == '*':
            result = int(words[0])*int(words[2])
        elif words[1] == '/':
            result = int(words[0])/int(words[2])

expressions = input().split(', ')
 
c = calc()
next(c)
 
for e in expressions:
    print(c.send(e))
 
c.close()
반응형

'WEB > Python-Django' 카테고리의 다른 글

Python 정규 표현식  (0) 2025.05.31
Python 데코레이터  (0) 2025.05.31
Python 이터레이터  (0) 2025.05.31
Python 메서드  (0) 2025.05.31
Python 제너레이터  (0) 2025.05.31