ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Use the arrow keys on your keyboard to control the snake. If the snake touches the edge of the terminal, game over. Ages 3-7, or if you are bored, alter it for practice.
Code:
#!/usr/bin/python
import time, curses
def snake(screen):
curses.curs_set(0)
screen.nodelay(True)
directions = {
curses.KEY_UP: (-1, 0),
curses.KEY_DOWN: (1, 0),
curses.KEY_LEFT: (0, -1),
curses.KEY_RIGHT: (0, 1),
}
direction = directions[curses.KEY_RIGHT]
snake = [(0, i) for i in reversed(range(20))]
while True:
screen.erase()
try:
screen.addstr(*snake[0], '@', curses.A_BOLD)
except:
print('Game Over!')
time.sleep(1)
exit()
for segment in snake[1:]:
screen.addstr(*segment, '*')
snake.pop()
snake.insert(0, tuple(map(sum, zip(snake[0], direction))))
direction = directions.get(screen.getch(), direction)
screen.refresh()
time.sleep(0.04)
if __name__ == '__main__':
curses.wrapper(snake)
File "snake.py", line 22
screen.addstr(*snake[0], '@', curses.A_BOLD)
SyntaxError: only named arguments may follow *expression
You've just discovered that Python 3 is incompatible with Python 2.
Check your /usr/bin/python symlink; it probably points to python2.7. But as changing it might cause breakage in other places, I'd recommend changing the shebang in line 1 to #!/usr/bin/python3.
Yup. python3. Python2 is EOL for a while. I haven't made anything in python2 in years. There is still python2 software running on some machines. Anything written in python today should be python3 I think.
I don't even have python2 installed. So I can't alter that and test it.
Quote:
I'll try it out there one day.
You did not miss anything. I was working on something else and made myself a little helper script to reference.
The last release of Python 2 was EOLed a year ago (20th April 2020). The current stable Debian still has Python 2 at /usr/bin/python (with Python 3 being at /usr/bin/python3).
Making snake.py Python 2 compatible is simple enough - as per the error a de-referenced list can only occur before named arguments, so one fix is to split snake[0] into two variables first then pass those in, i.e:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.