Skip to content Skip to sidebar Skip to footer

Valueerror When Trying To Convert Strings To Floating Point

I'm having troubles running this program. It tells me I have a ValueError: could not convert string to float The problem is though, that it just skips my input commands and jumps

Solution 1:

This is the desired code. I changed a little bit some indentations are wrong in case of contin() function and some logic. Please refer to this if I am wrong in some place do tell me. Thank you

def calc(num1,chars,num2):        #The function performing calculation. 
    if chars == "+":
        result = num1 + num2
        print (result)
        return result
    elif chars == "-":
        result = num1 - num2
        print(result) 
        return result 
    elif chars == "*":
        result = num1 * num2
        print(result)
        return result 
    elif chars == "/":
        result = float(num1) / float(num2)
        print(result)
        return result 
    else:
        print("Invalid or unsupported operation")

cont = ""  
def contin(res):
        num1 = res
        print("Operate? y/n: ")
        cont = raw_input()  
    if cont == "y": 
            print(num1)           # output is:                  ought to be:
            chars = raw_input()                   #result                     result
            num2 = float(input()) 
            num1=calc(num1,chars,num2)        #result                     operate y/n
            print num1     

    elif cont == "n":
            result = 0
            print(result)
    else:
            print ("Invalid response.")


num1 = float(input ()) 
chars = raw_input () 
num2 = float(input ()) 
result = 0       

while num1 > 0 or num2 > 0: 
    res = calc(num1,chars,num2)
    contin(res)
    break
if num1 == 0 and num2 == 0:
    print("Zero or undefined.")

Post a Comment for "Valueerror When Trying To Convert Strings To Floating Point"