【发布时间】:2015-10-19 21:16:15
【问题描述】:
我有一个计算器(python 3.4.2),可以使用 eval 进行正常操作。
def calculator(user_input):
if any(c not in config.valid_cal_chars for c in user_input):
print("- Invalid Equation | Bad characters")
return
elif not any(c in user_input for c in "0123456789"):
print("- Invalid Equation | No numbers found")
return
sys.stdout.write("calculating " + "-".join(gfx.load_sequence))
time.sleep(0.1)
print (" | 100%")
try:
current_ans = eval(user_input)
except (SyntaxError, ZeroDivisionError, NameError, TypeError, ValueError):
print ("- Invalid Equation | Error")
return
config.ans = current_ans
print (current_ans)
这里是config.ans、config.valid_cal_char所指的config.py:
ans = ("0.0")
valid_cal_chars = ("0123456789-+/*ansqrt() \n")
如果你想知道是什么
user_choice
变量指的是,它指的是我在这个函数之前的一个输入函数。该部分有效,因此无需担心。
但是,我想知道是否可以这样做:
input equation here: 4*4 #this would be saved as the user_input variable
> 16 #the output of the equation
input equation here: sqrt(ans) #this would use the previous answer saved in config.ans (ans) and the sqrt() to find the square root of the previous printed value, so:
> 4
所以键入 ans 会导致:
input equation here: 1+1
> 2
input equation here: ans
> 2
使用 sqrt() 会导致:
input equation here: 2+2
> 4
input equation here: sqrt(4)
> 2
所以如果还是不明白,sqrt() 求输入值的平方根。 ans 使用之前的返回值。因此,将两个“sqrt(ans)”结合起来将得到前一个返回值的平方根。
有了背景信息,我想做的是允许用户在计算时使用这些信息。虽然“eval”可能不起作用,但我也很乐意使用“exec”(知道危险)。但是,这里有一个 multitool.py(主文件),它导入这个文件(functions.py)来使用我在其中的所有功能,包括这个。
import os, sys, glob, math, random, login, gfx, config, functions, time
path = "******" #creates path to folder (can be changed by commenting this line out and creating new one)
dirs = os.listdir( path ) #not used currently
functions.load_sequence_complete()
functions.username_login()
time.sleep(0.05)
functions.password_login()
print ("\n[credentials have been verified! proceeding to main program " + "-".join(gfx.load_sequence) + "]\n")
time.sleep(0.1)
program = True
while (program == True):
user_choice = functions.choice_selecter()
functions.validate_choice(user_choice)
如果您需要任何其他信息,请将其放在下面的 cmets 或答案中,以便我可以对其进行编辑以帮助您:)
【问题讨论】:
标签: python function python-3.x eval calculator