Python - 将String转换为Integer以进行边界输入 [英] Python - Converting String to Integer for boundary inputs

查看:1773
本文介绍了Python - 将String转换为Integer以进行边界输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让边界输入在我的小游戏中运行。

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

所以我意识到我必须将从条目小部件中获得的字符串转换为整数,我查找了在stackoverflow和web上的其他地方执行的代码,但它似乎没有工作。

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.

我试过了两个:

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

int (entryx)
int (entryy)

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?

提前致谢!

推荐答案

以下陈述:

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

不会将整数值分配给 entryx entryy 。也许你想要这样的东西:

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

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

这篇关于Python - 将String转换为Integer以进行边界输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆