-3

My program is supposed to solve equations. I just added the part that distinguishes + and -. These equations are super easy yet. (like 2x = 4+2 or 4x = 1-4) here is my code:

from tkinter import *
import time
form = Tk()
form.title('Hello')
auff = Label(form, text='Put in Equation!')
rein = Entry(form)
lein = Entry(form)
istg = Label (form, text='=')
stop = Button(form, text='Stop',  command=form.destroy)
start = Button (form, text='Compute')
erge = Label (form, text='')

r = rein.get()
l = lein.get()

def operation (event):
    if ('+' or '-' in l):
        if '+' in l:
            l1,l2 = l.split ('+')
            if ('x' not in l1 or l2):
                ll = l1 + l2
                return ll
        if '-' in l:
            l1,l2 = l.split ('-')
            if ('x' not in l1 or l2):
                ll = l1 - l2
                return ll
    if ('+' or '-' in r):
        if '+' in r:
            r1,r2 = r.split ('+')
            if ('x' not in r1 or r2):
                rr = r1 + r2
                return rr
        if '-' in r:
            r1,r2 = r.split ('-')
            if ('x' not in r1 or r2):
                rr = r1 - r2
                return rr

def einfach (event):
    r = rein.get()
    l = lein.get()
    x = 'x'
    if x in l:
        operation (event)
        lr = int(lr)
        end = rr/lr
        end = int (end)
        erge['text']= 'x = {}'.format(end)
    elif x in r:
        operation (event)
        rl = int(rl)
        end = ll/rl
        end = int(end)
        erge['text']='x = {}' .format(end)

start.bind('<Button-1>', einfach)
auff.grid(row = 0, column = 1)
rein.grid(row = 1, column = 2)
istg.grid(row = 1, column = 1)
lein.grid(row = 1, column = 0)
erge.grid(row = 4, column = 1)
start.grid(row = 2, column = 1)
starts.grid(row = 3, column = 1)
erkl1.grid(row = 5, column = 1)
erkl2.grid(row = 6, column = 1)
erkl3.grid(row = 7, column = 1)
erkl4.grid(row = 8, column = 1)
stop.grid(row = 9, column = 1)


start.mainloop()

I may add the Error: There are no error messages but when you press compute it doesn't do anything.

Thanks in advance,

A Banana

Banana
  • 21
  • 1
  • 4
  • There are _lots of_ problems in this code. You should write your codes litlle-by-little and test instead writing all and try to run. – Lafexlos Jul 26 '17 at 09:17
  • As a starting point though, first thing to fix is: You are getting the values `r` and `l` right after you start GUI so both `r` and `l` are empty. You should `get` those values inside of `einfach` method. – Lafexlos Jul 26 '17 at 09:18
  • Your code raises `NameError` here. – Stop harming Monica Jul 26 '17 at 09:22
  • Also, `while '+' or '-' in l:` is the same as `while ('+') or ('-' in l)`, so it's not doing what you think it's doing. Same with `if 'x' not in l1 or l2:` which is the same as `if ('x' not in l1) or (l2)`. – Bryan Oakley Jul 26 '17 at 11:12
  • Thanks for the Advice but I still have a problem! But now I think I found another error! There is a problem getting r/l into split bc they are not strings! I already tried str (l/r) but it says l/r isn't assigned yet. – Banana Jul 27 '17 at 08:47

1 Answers1

0

there are several issues with you code.

The way you are writing your while statements does not work in the way you are expecting.

The statement:

while '+' or '-' in l:

does not mean while a + or a - is in l then do something. Instead it means while + is True or - is in l then do something. Because + is always going to be True then this while statement will always be active.

What you should write is:

while '+' in l or '-' in l:

This is along the lines of: while + in l or while - in l then do something.

You might want to take a look at some stack overflow post related to solving for x.

here is one post you might find helpful How can I solve equations in Python?

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79