Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Why does my programming suck? [Python]

Name: Anonymous 2006-08-26 1:02

Here I just wrote this. Its supposed to be a python curses dialogue writer. Are there experts here that can point out some obvious flaws or tell me to GTFO?

[code]
#!/usr/bin/env python


import sys, re
import string
import curses, traceback




def main(stdscr,file):
    global screen
    global textbox
    global y
   
    #screen = stdscr.subwin(23, 79, 0, 0)
    screen = stdscr.subwin(18, 79, 0, 0)
    textbox = stdscr.subwin(6 ,79, 18, 0)
    textbox.box()
    screen.refresh()
    y=1
    characters = create_characters()
    osymbol, oline = None, None
    while True:
        try:
            symbol, line = seperate_key(textbox_input())
        except TypeError:
            screen_message("No key entered!")
       
        try:
            characters[symbol]
            key_option(symbol, line, characters)
            #screen_debug(str(screen.getyx()[0]))
           
            if screen.getyx()[0] >= 14:
                clear_screen()
                key_option(symbol, line, characters)
           

        except KeyError:
            screen_message("Invalid key, try another.")
            key_option(osymbol, oline, characters)
       
        if osymbol != symbol:
            write_line(file,symbol, line,characters)

        osymbol = symbol
        oline = line
       
        screen.refresh()
       

def y_value():
    global y
    y+=1
    return y

def x_valadd(value):
    return len(value)+1

def textbox_input():
    textbox.erase();textbox.box()
    text =textbox.getstr(1,1)
    return text

def clear_screen():
    global y
    screen.erase()
    screen.refresh()
    y=0

def screen_message(message):
    clear_screen()
    screen.addstr(y_value(),1,message)
    screen.refresh()
  
def screen_debug(message):
    screen.addstr(y_value(),1,message)
    screen.refresh()

def character_says(symbol, line, characters):
    y = y_value()
    screen.addstr(y, 1, characters[symbol], curses.A_STANDOUT)
    screen.addstr(y, x_valadd(characters[symbol]), '   - ')
    screen.addstr(y, x_valadd(characters[symbol]+'   - '), '"%s"' %line, curses.A_BOLD)
    y_value()
   
def narrator_speech(speech):
    y = y_value()
    screen.addstr(y, 1, speech, curses.A_UNDERLINE)
   
    y_value()

def seperate_key(line):
    line = string.split(line, ' ')
    speech = ''
    for i in line[1:]:
        speech += "%s " %i
    if len(line) == 1:
        return None
    else:
        key=line[0][0]
        line=speech[0:-1]
        #key_option(key,line)
        return key,line
       
def key_option(key,line,characters):
    if key == '-':
        narrator_speech(line)
    elif key=='q':
        exit()
        #clean_quit('File saved to %s' %sys.argv[-1])
    else:
        character_says(key, line, characters)

def create_characters():
    screen.addstr(y_value(),0,">> Input Character names, followed by their associated symbol")
    screen.addstr(y_value(),0,">> When done press Enter")
   
    screen.refresh()
    y = y_value()
    characters={}
    while True:
        y+=1
        c = textbox_input()
        if not c:break
        c = string.split(c, ' ')
        try:
            characters[c[1]]=c[0]
            screen.addstr(y,1,c[0], curses.A_BOLD)
            screen.addstr(y,len(c[0])+3, "symbol - " +c[1])
            screen.refresh()
        except IndexError:
            screen_message("No key entered. Enter a key. An example is 'Jimmy j'")
   
    clear_screen()
    characters = add_sysChars(characters)
    return characters

def add_sysChars(characters):
    characters['-']=""
    characters['q']=''
    return characters


def write_line(file, key, speech, characters):
    file.write('%s - "%s"\n\n' %(characters[key], speech))
   

def clean_quit(message):
    exit()
    print
    print message
    print

def debug(message=None):
        exit()
        print
        print message
        print
        traceback.print_exc()

def exit():
    stdscr.keypad(0)
    curses.echo()
    curses.endwin()
   
   


if __name__=='__main__':
    try:
        # Initialize Curses
        stdscr=curses.initscr()
        stdscr.keypad(1)
        try:
            file = open(sys.argv[1], 'w')
            main(stdscr,file) #Enter the main loop
        except IndexError:
            exit()
            print
            print "You need a file! %s SCRIPT_FILE is fine too." %sys.argv[0]
            print
        except:
        # In the event of error
        debug()

Name: Anonymous 2006-08-26 1:06

Sorry for screwing up the formatting!

Name: Anonymous 2007-06-21 12:19 ID:D0GY813L

ONE WORD, THE FORCED INDENTATION OF CODE, THREAD OVER

Name: Anonymous 2007-06-21 12:21 ID:Heaven

>>3
ONE WORD, THE FORCED INTRODUCTION OF SHITTY MEMES, THREAD CONTINUES

Name: Anonymous 2007-06-21 12:35 ID:4scaHQvb

One word, the forced indentation of the code. Thread over.

Name: Anonymous 2007-06-21 13:00 ID:+5k+vXSt

I count a grand total of zero comments. Maybe that has to do with the quality of your programming?

Name: Anonymous 2007-06-21 17:52 ID:KjdPXjTv

Sorry for screwing up the formatting!

FUCKING WIN.

>>6
fail.

Name: Anonymous 2007-06-21 18:36 ID:qZGwUiae

ONE WORD, THE FORCED INDENTATION OF CODE, THREAD OVER

Name: Anonymous 2007-06-21 20:10 ID:3lCnk3aV

One word: Theforcedindentationofcode, Thread over

Name: Anonymous 2007-06-21 20:15 ID:/fHfXloF

one word, the fierce detention of cod, thread over

Name: Anonymous 2007-06-24 2:02 ID:GCE4MtDs

LOL Python :D

Name: Anonymous 2007-06-24 2:32 ID:90q2ACPT

Test

Name: Anonymous 2007-06-24 3:20 ID:Heaven

>>10
LOL

Name: Anonymous 2007-07-05 15:10 ID:Heaven

Q: Why does my programming suck?
A: [Python]

Name: Anonymous 2009-01-14 5:07

>>14
Agreed. Indentation is only useful when you treat code as well-formatted poem. IRL its flustrating and useless.

Name: Anonymous 2010-06-27 12:57

ur gay

Name: Anonymous 2010-06-27 13:51

my homework is to read the first chapter of SICP: Can someone do that for me please so I don't have to??

Name: Anonymous 2011-02-03 8:15

Name: Sgt.Kabuߑ៮kiman䃦鐊 2012-05-28 19:53

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List