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.

8
  • What exactly do you not understand about the error? Commented Jan 3, 2018 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. Commented Jan 3, 2018 at 13:42
  • so how to make it integer Commented Jan 3, 2018 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. Commented Jan 3, 2018 at 13:44
  • ok i got that how to solve it Commented Jan 3, 2018 at 13:50

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

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, //.

2 Comments

(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!
Your answer was first (by some seconds), so at least I tip my hat to you :-}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.