简体   繁体   中英

How to make the if statement function properly?

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM