2014-04-24 120 views
-1

因此,对于这一门课程,我必须制作一个计算器。 我所拥有的一切工作,但对于分频功能,每当我运行它,我得到的错误Python - ValueError:无效文字float():4/

Traceback (most recent call last): 
    File "C:\Python27\Calculator.py", line 43, in <module> 
    val3 = Mult(val1, val2) 
    File "C:\Python27\Calculator.py", line 17, in Mult 
    val1 = float(val1) 
    ValueError: invalid literal for float(): 4/ 

这里是我的代码,我意识到,我可能会使用这样的东西,如获得操作数超出了许多不正当手段字符串,但我真的不知道任何其他方式。

def firstNu(fullLine, symbol): 
    return fullLine[0:fullLine.find(symbol)].strip() 
def secondNumber(fullLine, symbol): 
    return fullLine[fullLine.find(symbol) + len(symbol) : len(fullLine)].strip() 
def Add(val1, val2): 
    val1 = float(val1) 
    val2 = float(val2) 
    val3 = val1 + val2 
    return val3 
def Sub(val1, val2): 
    val1 = float(val1) 
    val2 = float(val2) 
    val3 = val1 - val2 
    return val3 
def Mult(val1, val2): 
    val1 = float(val1) 
    val2 = float(val2) 
    val3 = val1 * val2 
    return val3 
def Div(val1, val2): 
    val1 = val1 
    val2 = val2 
    val3 = val1/val2 
    return val3 

while True: 
    equat = raw_input() 
    if equat.find("+") == 1: 
     operand = ('+') 
     val1 = firstNu(equat, operand) 
     val2 = secondNumber(equat, operand) 
     val3 = Add(val1, val2) 
    elif equat.find("-") == 1: 
     operand = ('-') 
     val1 = firstNu(equat, operand) 
     val2 = secondNumber(equat, operand) 
     val3 = Sub(val1, val2) 
    elif equat.find("*"): 
     operand = ('*') 
     val1 = firstNu(equat, operand) 
     val2 = secondNumber(equat, operand) 
     val3 = Mult(val1, val2) 
    elif equat.find("/"): 
     operand = ('/') 
     val1 = firstNu(equat, operand) 
     val2 = secondNumber(equat, operand) 
     val3 = Div(val1, val2) 
    print(val1, operand, val2, "=", val3) 

在此先感谢

+4

很多问题:由于您使用'equat.find(...)== 1',您只支持1位数字。像'val1 = val1'这样的行应该是不必要的。你的'firstNu'和'secondNumber'函数应该被合并成一个更好的函数来返回多个值。 –

回答

0

我建议的东西代替firstNusecondNumber这样的:

def get_operands(equation, operator): 
    return [number.strip() for number in equation.split(operator)] 

然后分配,如:在你的方法

val1, val2 = get_operands('345 + 123', '+') 

优点:

  • 支持多个数字的数字
  • 支持各地数字间距和运营商
1

find()回报-1如果没有找到指定的字符串。 Python认为-1是一个'truthy'值(与0,None,[]这些是'falsey'的值相反),所以当找不到子串'*'时,equat.find("\*")正在评估为True。你的if语句看起来更像:

if equat.find("+") != -1: 

错误发生,因为,当你输入一个部门方程,equat.find("\*")计算结果为-1,这是TruefirstNu被称为与运营商'*',并fullLine.find(symbol)评估为-1。 Python通过从字符串末尾向后计数来处理负数字符串索引(列表索引处理方式相同),所以firstNu返回fullLine[0:-1],如果行类似于'4/5',则该数字为'4/'float()不知道如何将'4/'转换为数字,因此会引发您所看到的错误。

你也应该用什么取代firstNusecondNumber

def parseNumbers(fullLine, symbol): 
    symbol_ind = fullLine.find(symbol) 
    if symbol_ind == -1: 
     ## do some error handling 
    return fullLine[0:symbol_ind].strip(), fullLine[symbol_ind+1:].strip() 
1

您可以通过操作分裂避免了大量的是样板的第一个拿到3个组件,然后派遣到的方法之一,在operator模块,例如:

from operator import sub, add, div, mul 
import re 

for line in iter(raw_input, ''): # stop after just enter pressed 
    try: 
     num1, op, num2 = re.split('([-+/*])', line) 
     print { 
      '-': sub, 
      '+': add, 
      '/': div, 
      '*': mul 
     }[op](float(num1), float(num2)) 
    except ValueError: 
     print line, '-- not valid' 
相关问题