Python - Converting String to Integer for boundary inputs

寵の児 提交于 2019-12-13 07:31:43

问题


I've been working on trying to get boundary inputs working in a small game of mine.

At first I got this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
    return self.func(*args)
  File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 173, in     buttonclick_gamescreen
    if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 :
TypeError: unorderable types: str() > int()

So I realised that I had to convert the string I got from the entry widget into an integer, I looked up the code for doing on both stackoverflow and elsewhere on the web but it doesn't seem to be working.

I've tried both:

int (e1.get())
int (e2.get())

and

int (entryx)
int (entryy)

within

while pressed == 8 :
    int (e1.get())
    int (e2.get())
    entryx = e1.get()
    entryy = e2.get()
    answerx = answerlistx[randomimage]
    answery = answerlisty[randomimage]

    if entryx == answerx and entryy == answery:
        canvas.delete(images)
        randomimage = random.randrange(0,49+1)
        scorecounter = scorecounter + 1
        game = PhotoImage(file=imagelist[randomimage])
        images = canvas.create_image(30, 65, image = game, anchor = NW)
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''
    if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 :
        wrong = canvas.create_image(30, 65, image = outside, anchor = NW)
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''
    else:
        wrong = canvas.create_image(30, 65, image = incorrect, anchor = NW)
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''

Without any luck. This was from following what I've read so far but I'm still getting the same error from above. Could someone tell me what I'm doing wrong please?

Thanks in advance!


回答1:


This below statements:

int (e1.get())   # This is actually doing nothing.
int (e2.get())
entryx = e1.get()
entryy = e2.get()

doesn't assign the integer value to entryx or entryy. Perhaps you wanted something like this:

entryx = int (e1.get())
entryy = int (e2.get())



回答2:


Lines int (e1.get()) and int (e2.get()) do actually nothing. Instead you should convert e1.get() and e2.get() to int while assigning them to entryx and entryy:

entryx = int(e1.get())
entryy = int(e2.get())

int() doesn't convert items in place:

>>> s = "100"
>>> int(s)
100
>>> s
'100'
>>> type(s)
<type 'str'>


来源:https://stackoverflow.com/questions/18170406/python-converting-string-to-integer-for-boundary-inputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!