0

So, here is my script:

#Imports
import decimal
#variables
neweq = "neweq"
on = 1
#loop
while on > 0:
#equasion function
    def eq ():
        global b
        b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
        print ""
        print ""
        print ""
        print ""
        b = float(b)
        b = '%.3f'%(b)
        if (b==exit):
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            exit ("Thank you for using me :)")
#input funcution
    def inp ():
        a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  ")
        if (a==exit):
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            exit ("Thank you for using me :)")
        if (a == neweq):
            print ""
            print ""
            a = 0
            eq ()
            inp()
        if (b==a):
            print ""
            print "Yes, the answer is", a
            print ""
            print ""
            eq ()
        else:
            print ""
            print "No, the answer is not", a
            print ""
            print ""
            print "test line", b
            inp ()
#function calls
    eq()
    inp ()

The problem?

Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  2.0/4.0




Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  1/2

No, the answer is not 0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  102.0

No, the answer is not 102.0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  1.0/2.0

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  .500

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  0

No, the answer is not 0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  4.0/2.0

No, the answer is not 2.0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  2.0/4.0

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  0.500

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  

It should be returning 'yes, the answer is 0.5', however, it is not. Same with several other equasions. I can't figure out what is wrong with it, however, my suspicion is that it is the b = '%.3f'%(b) and that's where I needed help.

Thank you!

x otikoruk x
  • 422
  • 1
  • 6
  • 16
  • can u tell me, what input will be expected??? like only `float/float` – Hackaholic Nov 05 '14 at 04:48
  • @Hackaholic `1.0/3.0`, `1.0/3.0`, `2*(3/4+1)`, and some basic math such as `1+1`, and soon to be `3*(4/a-3)==4/(a-1)`. – x otikoruk x Nov 05 '14 at 04:57
  • 1
    A few pointers unrelated to the immediate issue you're having: Putting the functions in the loop actually redefines them every time - you can take them out to the top level. Comparing `b==exit` will not work - first, b is already a number; second, you're comparing to the function `exit` not string `"exit"`. You can use actual boolean values, so you probably want `on=True` and `while on:`. There are also other uncommon things in the script - I'd recommend you start with simpler steps and verify they work correctly before building more code around them. – viraptor Nov 05 '14 at 05:09
  • @Thecheater887 Check my code – d-coder Nov 05 '14 at 05:25

2 Answers2

1

There you go. I have edited your code. Let me know if this is what you wanted and I have no idea why you have used print "" which I removed it here because it was hurting my eyes! :P. Also please don't use exit as input from the user use "quit" preferably.Explanation can be found in comments.

#Imports
import decimal
#variables
neweq = "neweq"
on = 1
#loop
#equasion function
def eq ():
        global b
        b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
        if b == exit:
             exit ("Thank you for using me :)")
        else:
            b = float(b)        ## input converted into float.
            b = '%.3f'%(b)      ## after this b would be of type string
            b = float(b)        ## again converting into float to match with "a" in `inp()` 
#input funcution
def inp ():
        a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  ")
        if a == exit:
            exit ("Thank you for using me :)")
        if a == neweq:
            a = 0
            eq ()
            inp()
        if b == a:          ## if a == b should work now.  
            print "Yes, the answer is", a
            eq ()
        else:
            print "No, the answer is not", a
            print "test line", b
            inp ()
#function calls
eq()
inp ()

And if you were using print"" to avoid the clutter in work space then try like this

   print "\n"*5  ## You have 5 empty lines. Replace the number 5 as per your needs

This is more neat and pythonic.

d-coder
  • 12,813
  • 4
  • 26
  • 36
  • I wanted it to go in a infinite loop, which yes, there was a few bugs with it, and I didn't want a calculator, the `test line` was a debugging helper. I didn't want to build a calculator, I wanted an answer checker. A lot of code that worked against intention, however, you DID manage to solve my problem! Thank you! – x otikoruk x Nov 05 '14 at 05:38
  • @Thecheater887 You want me to put into infinite loop ? Want some explanation where you went wrong ? Also what was the reason behind `print""` ? – d-coder Nov 05 '14 at 05:42
  • Sorry! Got Ninj'd! I have it working now, but I wanted it in a loop so you didn't have to relaunch after every equasion. The `b = float(b)` `b = '%.3f'%(b)` `b = float(b)` is what completed the code. The `print ""`'s were so it didn't clutter up the workspace, like so you could see where you were. Your help solved my problem though, so it is greatly appreciated! – x otikoruk x Nov 05 '14 at 05:46
0

This bit looks suspicious, it takes (presumably) a string and tries to convert it to a float:

    b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
    b = float(b)
101
  • 8,514
  • 6
  • 43
  • 69
  • I needed to make it where it is capped at 3 decimal places, or I wouldn't need it to be a float. Could you suggest how to do that without a float? – x otikoruk x Nov 05 '14 at 04:43
  • Your script is doing something like this: `float("a**3 + 2*a + 5")`, which will fail. You'd need to take that string and compute it somehow (to a number, not a string) before trying to shorten the float. – 101 Nov 05 '14 at 04:46
  • >>> float('3.0/7.0') Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for float(): 3.0/7.0 – Hackaholic Nov 05 '14 at 04:46
  • cant do this in this way – Hackaholic Nov 05 '14 at 04:47
  • @figs ok, so I deleted that part, which actually broke the entire script, but now you can't answer in a short 3 place decimal as intended, for say `1.0/3.0`, you'd have to answer with a simplified fraction, or in this case `1.0/3.0` instead of `.333` or `0.333`, as intended. – x otikoruk x Nov 05 '14 at 04:53
  • Go back a step. You're asking the user to type in a fairly complex piece of text and expecting Python to automatically read it as the resulting number. It won't do that. You have to evaluate the equation first. Try looking at these http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string http://stackoverflow.com/questions/594266/equation-parsing-in-python – 101 Nov 05 '14 at 04:59