类型错误:预期为整数参数

时间:2018-06-24 05:53:53

标签: python

我正在google的帮助下用Python制作蛇游戏,但是我发现了一些我不理解为什么会发生这种错误的错误...我的代码是

import random
import curses

s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

snk_x = sw/4
snk_y = sh/2
snake = [
    [snk_y, snk_x],
    [snk_y, snk_x-1],
    [snk_y, snk_x-2]
]

food = [sh/2, sw/2]
w.addch(food[0], food[1], curses.ACS_PI)

key = curses.KEY_RIGHT

while True:
    next_key = w.getch()
    key = key if next_key == -1 else next_key

    if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]:
        curses.endwin()
        quit()

    new_head = [ snake[0][0], snake[0][1]]

    if key == curses.KEY_DOWN:
        new_head[0] += 1

    if key == curses.KEY_UP:
        new_head[0] -= 1

    if key == curses.KEY_LEFT:
        new_head[1] -= 1

    if key == curses.KEY_RIGHT:
        new_head[1] += 1

    snake.insert(0, new_head)

    if snake[0] == food:
        food = None
        while food is None:
            nf = [
                random.randint(1, sh-1),
                random.randint(1, sw-1)
            ]
            food = nf if nf not in snake else None
        w.addch(food[0], food[1], curses.ACS_PI)
    else:
        tail = snake.pop()
        w.addch(tail[0], tail[1], ' ')
        w.addch(snake[0][0], snake[0][1], curses.ACS_CKBOARD)

运行此代码时,我遇到了两个错误,即

Traceback (most recent call last):
File "snakegame.py", line 20, in <module>
w.addch(food[0], food[1], curses.ACS_PI)
TypeError: integer argument expected, got float

6 个答案:

答案 0 :(得分:2)

在Python 3中,/产生浮点结果。要获取整数结果,请使用整数除法//

food = [sh // 2, sw // 2]

此外,您的snake用浮点数初始化。同样,为了确保snake仅包含整数,请执行以下操作:

snk_x = sw // 4
snk_y = sh // 2

此后,您将看到由于new_head超过swsh(如果您不按任何键就让蛇自己移动)而导致的另一次崩溃,但这与原始问题。

答案 1 :(得分:0)

进行了一些修改,没有错误,但是食物没有第二次出现。

import random
import curses

s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

snk_x = sw/4
snk_y = sh/2
snake = [
    [snk_y, snk_x],
    [snk_y, snk_x-1],
    [snk_y, snk_x-2]
]

food = [sh/2, sw/2]
w.addch(int(food[0]), int(food[1]), curses.ACS_PI)

key = curses.KEY_RIGHT

while True:
    next_key = w.getch()
    key = key if next_key == -1 else next_key
    if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in       snake[1:]:
    curses.endwin()
    quit()

new_head = [snake[0][0], snake[0][1]]

if key == curses.KEY_DOWN:
    new_head[0] += 1
if key == curses.KEY_UP:
    new_head[0] -= 1
if key == curses.KEY_LEFT:
    new_head[1] -= 1
if key == curses.KEY_RIGHT:
    new_head[1] += 1

snake.insert(0, new_head)

if snake[0] == food:
    food = None
    while food is None:
        nf = [
            random.randint(1, sh-1),
            random.randint(1, sw-1)
        ]
        food = nf if nf not in snake else None
    w.addch(int(food[0]), int(food[1]), curses.ACS_PI)
else:
    tail = snake.pop()
    w.addch(int(tail[0]), int(tail[1]), ' ')

w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD)

enter image description here

答案 2 :(得分:0)

这有点修改,对我来说很好用。

import random
import curses

s=curses.initscr()
curses.curs_set(0)
sh,sw=s.getmaxyx()
w=curses.newwin(sh,sw,0,0)
w.keypad(1)
w.timeout(100)

snk_x=sw//4
snk_y=sh//2
snake=[
[snk_y,snk_x],
[snk_y,snk_x-1],
[snk_y,snk_x-2]

]

food=([sh//2,sw//2])
w.addch(food[0],food[1],curses.ACS_PI)
key=curses.KEY_RIGHT

while True:
 next_key=w.getch()
 key=key if next_key==-1 else next_key

  if snake [0][0] in [0,sh] or snake [0][1] in [0,sw] or snake[0] in snake [1:]:
    curses.endwin()
    quit()
new_head =[snake[0][0],snake[0][1]]

if key ==curses.KEY_DOWN:
    new_head[0]+=1
if key==curses.KEY_UP:
    new_head[0]-=1
if key==curses.KEY_LEFT:
    new_head[1]-=1
if key==curses.KEY_RIGHT:
    new_head[1]+=1
snake.insert(0,new_head)

if snake[0]==food:
    food=None
    while food is None:
        nf=[
        random.randint(1,sh-1),
        random.randint(1,sw-1)
        ]
        food=nf if nf not in snake else None
    w.addch(food[0],food[1],curses.ACS_PI)
else:
    tail=snake.pop()
    w.addch(tail[0],tail[1],' ')


w.addch(snake[0][0],snake[0][1],curses.ACS_CKBOARD)

答案 3 :(得分:0)

这应该很好

import random
import curses

s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

snk_x = int(sw/4)
snk_y = int(sh/2)
snake = [
    [snk_y, snk_x],
    [snk_y, snk_x-1],
    [snk_y, snk_x-2]
]

food = [sh/2, sw/2]
w.addch(int(food[0]), int(food[1]), curses.ACS_PI)

key = curses.KEY_RIGHT

while True:
    next_key = w.getch()
    key = key if next_key == -1 else next_key

    if snake[0][0] in [0, sh] or snake[0][1]  in [0, sw] or snake[0] in snake[1:]:
        curses.endwin()
        quit()

    new_head = [snake[0][0], snake[0][1]]

    if key == curses.KEY_DOWN:
        new_head[0] += 1
    if key == curses.KEY_UP:
        new_head[0] -= 1
    if key == curses.KEY_LEFT:
        new_head[1] -= 1
    if key == curses.KEY_RIGHT:
        new_head[1] += 1

    snake.insert(0, new_head)

    if snake[0] == food:
        food = None
        while food is None:
            nf = [
                random.randint(1, sh-1),
                random.randint(1, sw-1)
            ]
            food = nf if nf not in snake else None
        w.addch(food[0], food[1], curses.ACS_PI)
    else:
        tail = snake.pop()
        w.addch(int(tail[0]), int(tail[1]), ' ')

    w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD)

答案 4 :(得分:0)

其他解决方案在需要的更多地方过度使用int()。您只需要在显示的位置使用//整数除法即可。

也更有趣。钻石背蛇。

import random
import curses

s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

snk_x = sw//4 # use integer division here
snk_y = sh//2 # use integer division here
snake = [
    [snk_y, snk_x],
    [snk_y, snk_x-1],
    [snk_y, snk_x-2]
]

food = [sh//2, sw//2] # use integer division here
w.addch(food[0], food[1], curses.ACS_PI)

key = curses.KEY_RIGHT

while True:
    next_key = w.getch()
    key = key if next_key == -1 else next_key

    if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]:
        curses.endwin()
        quit()
    
    new_head = [snake[0][0], snake[0][1]]

    if key == curses.KEY_DOWN:
        new_head[0] += 1
    if key == curses.KEY_UP:
        new_head[0] -= 1
    if key == curses.KEY_LEFT:
        new_head[1] -= 1
    if key == curses.KEY_RIGHT:
        new_head[1] += 1

    w.addch(snake[0][0], snake[0][1], curses.ACS_DIAMOND)
    snake.insert(0, new_head)

    if snake[0] == food:
        food = None
        while food is None:
            nf = [
                random.randint(1, sh-1),
                random.randint(1, sw-1)
            ]
            food = nf if nf not in snake else None
        w.addch(food[0], food[1], curses.ACS_PI)
    else:
        tail = snake.pop()
        w.addch(tail[0], tail[1], ' ')

    w.addch(snake[0][0], snake[0][1], curses. ACS_CKBOARD)

答案 5 :(得分:0)

使用 // not / 因为在您想要插入整数值时解释在浮点值中看到您的参数

snk_x = sw//4
snk_y = sh//2
snake = [
    [snk_y, snk_x],
    [snk_y, snk_x-1],
    [snk_y, snk_x-2],]
food = [sh//2, sw//2]