0

The if statement doesn't work.It just goes straight to the else and prints incorrect even if you put in the right values. The only reason it would go straight to the else and print incorrect is if the values were not equal to the ones in the condition.

from Tkinter import *
import tkMessageBox

app = Tk()
# Message Window

def messagePop():
    get_data()
    tkMessageBox.showinfo('Results', '100% Very Good')

# Background colour

app.configure(bg='gray')



# The position and size relative to the screen
app.geometry('500x500+450+140')

# The title of the program
app.title('Maths4Primary')

# The icon
app.wm_iconbitmap('MathIcon.ico')

# Object positioning in the program
# def GridPos:

# I might use the place() method for the screen layout.
Label(app, text="Put these prices in order", bg="gray", fg="blue").place(x=100,y=10)

Label(app, text= u"\xA3" + "20.50", bg="gray", fg="blue").place(x=50,y=35)

Label(app, text=u"\xA3" + "2.50", bg="gray", fg="blue").place(x=200,y=35)

Label(app, text= u"\xA3" + "0.25", bg="gray", fg="blue").place(x=350,y=35)

# Entry






global x_data,y_data,z_data                       #----------add this

def get_data():
    global x_data,y_data,z_data
    x_data = x.get()
    y_data = y.get()
    z_data = z.get()
    print "x_data = {0} , y_data = {1} , z_data = {2}".format(x_data,y_data,z_data)

def messagePop():
    get_data()
    #---your Entry, which YOU NEED HELP ON THIS PART 
    if (x_data==0.25) and (y_data==2.5) and (z_data==20.5):   #----------compare here
        print("Well done")
      #  tkMessageBox.showinfo('Results', '100% Very Good')

    else :
        print ("Incorrect")










x = Entry(app)
y = Entry(app)
z = Entry(app)

x.place(x=50,y=60)
y.place(x=200,y=60)
z.place(x=350,y=60)

# Buttons
B1 = Button(app,text='Marks',bg='gray99',fg='black', command = messagePop ).place(x=425,y=450)

app.mainloop()
40_WPM
  • 43
  • 1
  • 5
  • Hmmm, let's see. Python has been around since the early 90s, is extensively used by major companies or organisations like Google, ILM and the Nasa, and is a key component of most if not all Linux distros... And you really think something like the `if` statement could be broken ? – bruno desthuilliers Sep 26 '13 at 11:40
  • I mean the if statement that I created. Ofcourse a properly coded if statement should function without fault, but as for mine it has some problems. – 40_WPM Sep 28 '13 at 23:02

1 Answers1

1

You are comparing strings to floating point values. They'll never be the same because they are not the same basic type.

Compare with strings:

if x_data == "0.25" and y_data == "2.5" and z_data == "20.5":

or convert x_data, y_data and z_data to floats first. Do note that floating point comparisons are also fraught with problems as floating point numbers have a finite limit to their precision. See floating point equality in Python and in general for example.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343