获取数字
Coddy Python 计算器项目 课程的第 18 课,共 29 课。
现在,我们想把字符串解析为列表表示形式,以便将其作为输入传给 eval。
例如,32 +12* 4 应解析为 ['+', 32, ['*', 12, 4]]
我们先从一个辅助函数开始,它会帮助我们编写主要的解析函数。
挑战
简单创建一个函数 get_next,它接收一个不含空格的字符串和一个数字(表示字符串中的起始索引),并返回下一个数字或运算符。
这次只处理数字。
示例:
get_next('32+12*4', 0)->32get_next('1.2*3.54', 4)->3.54get_next('3/56-2', 3)->6
你可以假设起始索引位于数字上。
注意:数字有两种类型,int 或 float。
确保返回的数字类型是 int 或 float,而不是 str!
亲手试试
def calc(op, n1, n2=None):
if not isinstance(n1, int) and not isinstance(n1, float):
raise Exception('Invalid number "' + str(n1) + '"')
if n2 is None:
if op == '+' or op == 'add':
return n1
if op == '-' or op == 'sub':
return -n1
raise Exception('Invalid operator "' + op + '"')
if not isinstance(n2, int) and not isinstance(n2, float):
raise Exception('Invalid number "' + str(n2) + '"')
if op == '+' or op == 'add':
return n1 + n2
if op == '-' or op == 'sub':
return n1 - n2
if op == '*' or op == 'mul':
return n1 * n2
if op == '/' or op == 'div':
if n2 == 0:
raise Exception("Division by zero")
return n1 / n2
if op == '%' or op == 'mod':
if n2 == 0:
raise Exception("Division by zero")
return n1 % n2
if op == '^' or op == 'pow':
return n1 ** n2
raise Exception('Invalid operator "' + op + '"')
def eval(lst):
if not isinstance(lst, list):
raise Exception('Failed to evaluate "' + str(lst) + '"')
if len(lst) == 2:
op = lst[0]
n = lst[1]
if not isinstance(n, list):
return calc(op, n)
if isinstance(n, list):
n = eval(n)
return calc(op, n)
elif len(lst) == 3:
op = lst[0]
n1 = lst[1]
n2 = lst[2]
if not isinstance(n1, list) and not isinstance(n2, list):
return calc(op, n1, n2)
if isinstance(n1, list):
n1 = eval(n1)
if isinstance(n2, list):
n2 = eval(n2)
return calc(op, n1, n2)
else:
raise Exception('Failed to evaluate "' + str(lst) + '"')
LEVEL_TWO_OPERATORS = ['+', '-', 'add', 'sub']
LEVEL_ONE_OPERATORS = ['*', '/', '%', 'mul', 'div', 'mod']
FUNCTIONS_OPERATORS = ['^', 'pow']
def struct(lst):
if not isinstance(lst, list) or len(lst) <= 1:
raise Exception('Failed to structure "' + str(lst) + '"')
if len(lst) == 2:
return lst
lst_cpy = lst
operators_groups = [FUNCTIONS_OPERATORS, LEVEL_ONE_OPERATORS, LEVEL_TWO_OPERATORS, 'all']
for operators_group in operators_groups:
i = 0
while i < len(lst):
if (not isinstance(lst[i], int) and not isinstance(lst[i], float) and not isinstance(lst[i], list)) if operators_group == 'all' else (lst[i] in operators_group):
if len(lst) - 1 == i:
raise Exception('Failed to structure "' + str(lst_cpy) + '"')
if len(lst) == 3:
return [lst[i], lst[i - 1], lst[i + 1]]
temp = lst[:i - 1]
temp.append([lst[i], lst[i - 1], lst[i + 1]])
temp += lst[i + 2:]
lst = temp
else:
i += 1
return lstPython 计算器项目中的所有课
自己练习: Python 在线编译器