2014-04-24 118 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' 
相關問題