简体   繁体   English

如何使if语句正常运行?

[英]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. if语句不起作用,即使您输入正确的值,它只会直接转到else并打印不正确。 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. 它直接进入else并打印不正确的唯一原因是,如果值不等于条件中的值。

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. 或将x_datay_dataz_data转换为浮点数。 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. 例如,请参见Python中的浮点等式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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