#5 + 5 *10 = 순차적 계산으로 만들자
import os
os.system("cls")
operator = ["+", "-", "*", "/", "="]
def string_calculator(user_input, show_history = False):
string_list = []
lop =0 #마지막 오퍼레이터
if user_input[-1] not in operator:
user_input += "="
for i,s in enumerate(user_input) : #enumerate란 인자번호와 값리턴함 튜플형태로
if s in operator:
if user_input[lop:i].strip() != "":
string_list.append(user_input[lop:i])
string_list.append(s)
lop = i+1
string_list = string_list[:-1]
pos = 0
while True:
if pos +1 > len(string_list):
break
if len(string_list) > pos +1 and string_list[pos] in operator:
temp = string_list[pos-1] + string_list[pos] + string_list[pos+1]
del string_list[0:3]
string_list.insert(0,str(eval(temp)))
pos =0
if show_history:
print(string_list)
pos +=1
if len(string_list)>0:
result = float(string_list[0])
return round(result,4)
while True:
user_input = input("계산식을 입력하세요>>")##" #분리해서 리스트 만들자
if user_input == "/exit":
break
result = string_calculator(user_input, show_history=True)
print("결과 : {}".format(result))
os.system("pause")
'인공지능 공부 > 남박사의 파이썬 실전' 카테고리의 다른 글
(인프런) 07. 파이썬 실전 스마트 타자연습기 만들기 (1) | 2021.05.27 |
---|---|
(인프런) 06. 간단한 한컴타자연습 만들기 (0) | 2021.05.26 |
(인프런) 04. 파이썬 실전 간단한 콘솔 계산기 (단순) (0) | 2021.05.25 |
(인프런) 03. 파이썬 실전 영어단어맞추기 게임 (0) | 2021.05.25 |
(인프런) 02. 파이썬 실전 숫자야구게임 (0) | 2021.05.25 |