0

I am attempting to create an ASCII level editor in Python using curses but I'm having issues. I get Traceback (most recent call last): File "lvleditor_curses.py", line 36, in <module> editor.newLine() File "lvleditor_curses.py", line 31, in newLine self.stdscr.addstr(self.level[0][0]) TypeError: expect bytes or str, got int when using the following code.

import os, curses

class Editor:

  def __init__(self):
    self.stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    self.stdscr.keypad(True)

    self.stdscr.addstr("test")
    self.stdscr.refresh()


    self.level = []

  def newLine(self):
    line = self.stdscr.getstr()

    self.level += [list(line)]
    self.stdscr.addstr(self.level[0][0])
    self.stdscr.refresh()


editor = Editor()
editor.newLine()
Jordan Baron
  • 3,752
  • 4
  • 15
  • 26

2 Answers2

1

I just had this issue now. The getstr function returns a bytearray by default. Therefore you must cast the bytearray to a string. Add:

line = line.decode()

Under:

line = self.stdscr.getstr()
James
  • 274
  • 4
  • 12
0

Clearly, self.level[0][0] is of the wrong type (int). Cast it to string with str(self.level[0][0]).

TwistedSim
  • 1,960
  • 9
  • 23