0

I have made a snakes game in Python which was referred from someplace, but am getting error.

The program snakes.py is given below:

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)

I am getting the following error on w.addch(food[0], food[1], curses.ACS_PI):

TypeError: integer argument expected, got float.

I am also usng python 3.6 version, so how to fix this code.

wahab memon
  • 2,193
  • 2
  • 10
  • 23
  • What exactly do you not understand about the error? – Jonathon Reinhart Jan 03 '18 at 13:42
  • What part of the message are you having problems understanding? An integer was expected, but it got a float. You're passing a float (a number with a decimal point and numbers to the right of it) where it's expecting you to pass an integer. Depending on how you declared `food`, those values may be floats. `curses.ACS_PI` definitely is a float. Fix the code by not passing a float where an integer is expected. You have the type definitions and code right in front of you. – Ken White Jan 03 '18 at 13:42
  • so how to make it integer – wahab memon Jan 03 '18 at 13:43
  • 1
    I'm not sure how you can write all of the code and logic above and not know how to convert to an int. – Mark Riddell Jan 03 '18 at 13:44
  • ok i got that how to solve it – wahab memon Jan 03 '18 at 13:50
  • @MarkoPolo Because he **didn't write the code**, and stole it from here: https://github.com/ebrian/engineerman/blob/master/015/snake.py – Jonathon Reinhart Jan 03 '18 at 13:53
  • *"I have made a snakes game..."* If you're going to steal someone else's code, and then lie and say that you wrote it, you should at least be smart enough to realize it was written for Python 2. – Jonathon Reinhart Jan 03 '18 at 13:56
  • Thats why I mentioned that am using Python 3.6. And yes am learning Python, so I was actually referred from the above link you provided – wahab memon Jan 03 '18 at 13:58

2 Answers2

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

In Python 3, the division / operator always produces floating-point values.

You should consider using the integer division operator, //.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • *(You deleted your commentwith another question, but I will leave this one.)* We are not going to debug your entire application for you. Add `print` statements, or use `pdb` and figure out where the float values are coming from. You can do this! – Jonathon Reinhart Jan 03 '18 at 13:49
  • Your answer was first (by some seconds), so at least I tip my hat to you :-} – Alfe Jan 03 '18 at 14:13
2

In Python 3 the slash operator produces a float, even if both its arguments were ints. So your line food = [sh/2, sw/2] produces a list of two floats.

You should use the double-slash operator which produces an int:

food = [sh//2, sw//2]
Alfe
  • 56,346
  • 20
  • 107
  • 159