Python Forum
Python error. Can't figure it out.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python error. Can't figure it out.
#1
Hi.

I'm trying to write a BBS lister script in Python (obviously). But, whenever I try to execute the script, I get this error:

File "./proj.py", line 14
c = 0;
^
IndentationError: expected an indented block

I've made sure that the indentation is correct, yet I still get this error.
Can someone please tell me what's wrong with my code?

Thank you.



#!/usr/bin/env python

from os import system
import curses
import sys

from sites import sites

commands = [" add         ", " connect     ", " information ", " help       ", " quit        "];

def execute_cmd(cmd_string):
def character(stdscr):

c = 0;
screen = curses.initscr();
while c != ord('q') and c != ord('Q'):

    attributes = {}
    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
    attributes['normal'] = curses.color_pair(1)

    curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE)
    attributes['highlighted'] = curses.color_pair(2)

    c = 0  # last character read
    option = 0  # the current option that is marked
    option2 = 0
    while c != 10:  # Enter in ascii
        stdscr.erase()
        for i in range(len(commands)):
            if i == option:
                attr = attributes['highlighted']
            else:
                attr = attributes['normal']
            stdscr.addstr("{0}. ".format(i + 1))
            stdscr.addstr(commands[i] + '', attr)

        for j in range(len(sites)):
            if j == option2:
                attr = attributes['highlighted']
            else:
                attr = attributes['normal']
            stdscr.addstr("{0}. ".format(j + 1))
            stdscr.addstr(sites[j] + '\n', attr)

        c = stdscr.getch()

        if c == curses.KEY_LEFT and option > 0:
            option -= 1
        elif c == curses.KEY_RIGHT and option < len(commands) - 1:
            option += 1

        if c == curses.KEY_UP and option2 > 0:
            option2 -= 1
        elif c == curses.KEY_DOWN and option < len(sites) - 1:
            option2 += 1

        elif c == 10 and option2 == 1:
            i = option;
            screen.clear();
            curses.endwin();
            execute_cmd( sites[j][1] + " " +  sites[j][2] );
            #break;
        elif c == 10 and option2 == 0:
            i = option;
            screen.clear();
            curses.endwin();
            sysopname = raw_input("^[[2J^[[H^[[1;34mname of sysop^[[0;34m: ^[[0;37m" )
            type(sysopname);
            bbsname = raw_input("^[[1;34mname of bbs^[[0;34m: ^[[0;37m" )
            type(bbsname);
            address = raw_input("^[[1;34mtelnet address^[[0;34m: ^[[0;37m")
            type(address);
            software = raw_input("^[[1;34msoftware^[[0;34m: ^[[0;37m");
            type(software);
            saveFile = open("bbslist","a")
            file = open("sites.py", "a+")
            saveFile.write(sysopname)
            saveFile.write("\n")
            saveFile.write(bbsname)
            saveFile.write("\n")
            saveFile.write(address)
            saveFile.write("\n")
            saveFile.write(software)
            saveFile.write("\n")
            saveFile.close()
            system("cat sites.py | sed -e 's/];//g' > sites")
            system("mv sites sites.py")
            system("sed -i -e '$a ,' sites.py")
            file = open("sites.py", "a")
            file.write('["')
            file.write(bbsname)
            file.write('",  "telnet", ')
            file.write('"')
            file.write(address)
            file.write('"]')
            file.write('\r\n];')
            file.close()
        elif c == 10 and option2 == 2:
            i = option;
            screen.clear();
            curses.endwin();
            print("INFO");
        elif c == 10 and option2 == 3:
            i = option;

        elif c == 10 and option2 == 4:
            i = option;
            screen.clear();
            curses.endwin();


screen.clear();
curses.endwin();
curses.wrapper(character)
Reply
#2
When I try your code, it errors out on line 12, which makes sense because after a def Python expects indentation. So it looks like the error you posted isn't for the code you posted.
Reply
#3
No, indentation is obviously not correct. Look at lines 12 and following I don't know how many lines
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Oct-21-2018, 03:42 PM)micseydel Wrote: When I try your code, it errors out on line 12, which makes sense because after a def Python expects indentation. So it looks like the error you posted isn't for the code you posted.

Yes. You're right. I posted the wrong code. It does indeed error out on line 12. With:

  File "./proj.py", line 12
    def character(stdscr):
      ^

(Oct-21-2018, 03:42 PM)buran Wrote: No, indentation is obviously not correct. Look at lines 12 and following I don't know how many lines

Can you recommend a program that would auto indent my code?

Ok. I found a program to auto indent my code: autopep8. After using it with my code, I get the exact same error as before.
Reply
#5
The error is happening because the interpreter expects an indented line after the function signature. Do this and it will work:

def execute_cmd(cmd_string):
    pass

def character(stdscr):
    pass
Reply
#6
Thank you for that. But now, i'm getting this error:

Traceback (most recent call last):
File "./proj.py", line 21, in <module>
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
 _curses.error: must call start_color() first
Reply
#7
You need to indent your code yourself, in the general case there's no tool that can do it for you, it can just make things more consistent.

As for your problem, anytime you post a traceback you should include the exact code that is needed to reproduce it. (Ideally you would provide the minimum code needed to reproduce the problem, but we still need the exact code and exact traceback.)
Reply
#8
Ok. I'm attempting to start over and rewrite it.
The problem now is the

from sites import sites
line. It's not importing the sites script. Which is

sites = [
["PTT      ",     "ssh",    "[email protected]"],
["PTT2     ",    "ssh",    "[email protected]"],
["MathBBS  ", "telnet", "bbs.math.ntu.edu.tw"]
];
Here is the new code.

#!/usr/bin/env python

from os import system
import curses
import sys

from sites import sites

commands = [" add         ", " connect     ", " information ", " help       ", " quit        "];


def character(stdscr):
     attributes = {}
     curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
     attributes['normal'] = curses.color_pair(1)

     curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE)
     attributes['highlighted'] = curses.color_pair(2)

     c = 0  # last character read
     option = 0  # the current option that is marked
     option2 = 0
     while c != 10:  # Enter in ascii
         stdscr.erase()
         for i in range(len(commands)):
             if i == option:
                 attr = attributes['highlighted']
             else:
                 attr = attributes['normal']
             stdscr.addstr("{0}. ".format(i + 1))
             stdscr.addstr(commands[i] + '', attr)

         for j in range(len(sites)):
             if j == option2:
                 attr = attributes['highlighted']
             else:
                 attr = attributes['normal']
             stdscr.addstr("{0}. ".format(j + 1))
             stdscr.addstr(sites[j] + '\n', attr)
         c = stdscr.getch()
         if c == curses.KEY_LEFT and option > 0:
             option -= 1
         elif c == curses.KEY_RIGHT and option < len(commands) - 1:
             option += 1
         if c == curses.KEY_UP and option2 > 0:
             option2 -= 1
         elif c == curses.KEY_DOWN and option2 < len(sites) - 1:
             option2 += 1

     stdscr.getch()

curses.wrapper(character)
When I attempt to execute the code, I get this error:

Traceback (most recent call last):
  File "./test.py", line 57, in <module>
    curses.wrapper(character)
  File "/usr/lib64/python2.7/curses/wrapper.py", line 43, in wrapper
    return func(stdscr, *args, **kwds)
  File "./test.py", line 39, in character
    stdscr.addstr(sites[j] + '\n', attr)
TypeError: can only concatenate list (not "str") to list
Reply
#9
It is importing sites. Sites is not being utilized correctly. The index sites[j] returns a list because each item in sites is a list:

sites = [
["PTT      ",     "ssh",    "[email protected]"],
["PTT2     ",    "ssh",    "[email protected]"],
["MathBBS  ", "telnet", "bbs.math.ntu.edu.tw"]
];
So, sites[0] is ["PTT ", "ssh", "[email protected]"]. You cannot concatenate a list and a string. You can append the string or you can take any of the contents, which are all strings, and concatenate the additional string.
Reply
#10
Ok. I pretty much solved it. I had to do some rewriting of the code. I haven't finished it, but at least I accomplished something. Thank you to everyone who gave me advice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to figure out why I always get this error while trying to calculate zonal AndreiDaniel22 0 1,536 Sep-17-2021, 09:00 AM
Last Post: AndreiDaniel22
  saving only one line of a figure as an image (python matplotlib) nitrochloric 0 1,992 Nov-23-2020, 01:41 PM
Last Post: nitrochloric
  I cannot figure out this error ErnestTBass 8 3,053 Aug-20-2020, 08:06 PM
Last Post: ErnestTBass
  I cannot figure out this error ErnestTBass 8 3,425 Apr-24-2020, 06:03 PM
Last Post: ErnestTBass
  I cannot figure out this error ErnestTBass 6 2,551 Mar-28-2020, 04:58 AM
Last Post: SheeppOSU
  Can't figure out the syntax error, relatively simple code maho686868 3 3,083 Jul-08-2018, 03:43 PM
Last Post: volcano63
  Trying to figure out Python IRC Tass 0 1,912 Apr-23-2018, 11:06 PM
Last Post: Tass
  Figure with Python maria 2 3,321 Jun-13-2017, 05:59 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020