0

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 to

print("Invalid Response")

This program works fine on my cellphone but not on my windows 10 laptop.

Any help? Try running it and let me know if it works for you.

def calc():        #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():
        result = calc()
        print("Operate? y/n: ")
        cont = input()  
if cont == "y": 
        print(result)           # output is:                  ought to be:
        chars = input()                   #result                     result
        contin_num = float(input()) 
        calc(contin_num)        #result                     operate y/n
        print(result, chars, contin_num)     

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


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

while num1 > 0 or num2 > 0: 
    calc()
    contin()
    break
if num1 == 0 and num2 == 0:
    print("Zero or undefined.")
8
  • Your indentation is bad: your elif's have a different indentation than the if, and they also have less indentation than the rest of the code in the function. Fix the indentation then try again. Commented Aug 1, 2016 at 15:22
  • those are just adjustments I made because Stack Exchange insisted I have a four space indentation. It is not this way on my original program all if's and elif's are indented properly. Make the correction and try running it. Commented Aug 1, 2016 at 15:24
  • 1
    No, it is your responsibility to post code that corresponds to what you want to run. I do not know where you want your function calc() to end so it would be difficult for me to do the corrections. The usual way to post code is to copy-and-paste from your text file, highlight the code with your mouse or keyboard, then click the "code sample" icon (which looks like a pair of braces) in the question editor. This will easily add the four spaces to each line. Commented Aug 1, 2016 at 15:26
  • aye aye cap, give me a sec. I might have to send a link since the code is too long for a comment. Commented Aug 1, 2016 at 15:28
  • Why did you create a duplicate question instead of updating the previous one? Commented Aug 1, 2016 at 15:28

1 Answer 1

0

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.")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.