loops - Python Program to replace spaces with tabs, tabs with spaces, etc. Having problems -
loops - Python Program to replace spaces with tabs, tabs with spaces, etc. Having problems -
i'm having problems creating code program. here's outline of code supposed in end, i'm coding 1 function @ time not confuse myself further. (get_input) should not changed it's necessary program.
program outline: - programme unix style command line, using qualifiers -t or +t phone call functions
1) alter tabs in indenting spaces
2) alter spaces in indenting tabs
3) substitute spaces, tabs, , newlines printable characters
4) undo 3
functions not used @ same time
using command line qualifiers allow user specify functions use
using (-) sign introduce short qualifier
i working on 1 function of i'm having troubles getting read entire file provided. entire code of programme this:
#name: nathan geist #id #: 10152796 #tut: 06 #assignment 3 #code editing program: # 1) alter tabs in indenting spaces # 2) alter spaces in indenting tabs # 3) substitute spaces, tabs, , newlines printable characters # 4) undo 3 # *functions not used @ same time* # *using command line qualifiers allow user specify functions use* # *using (-) sign introduce short qualifier* # test tabbed stuff #imports import sys #defining constants eof = chr(4) # standard end of file character tab_char = chr(187) # ">>" character used create tab character visible space_char = chr(183) # raised dot used create space character visible newline_char = chr(182) # backwards p used create newline character visible #input def get_input(): """this function works input() (with no arguments) except instead of throwing exception when encountering eof homecoming eof character (char(4)) returns: line of input or eof if end of file""" try: ret = input() except eoferror: ret = ret+eof homecoming ret def file_generator(): output_string = str() while true: input_string = get_input() if(get_input == eof): break output_string = output_string + input_string homecoming output_string #tabs spaces def tab_to_space(): """ function changes tabs in indenting spaces """ s4 = " " * 4 while true: line = get_input() + ("\n") if line == eof: break result = line.replace(" ", s4) def space_to_tab(): pass def string_to_ascii(): pass def ascii_to_string(): pass def help_function(): """prints out help file program, telling user how utilize functions limitations of arguments""" print("\ \nthis programme processes python files in next ways:\n\ 1) changes tabs in indenting spaces\n\ 2) changes spaces in indenting tabs\n\ 3) substitute spaces, tabs, , newlines printable characters\n\ 4) undo actions of 3\n\ \nlegend:\n\ ---------------------------------------------------------------------\n\ +t (replaces sequences of spaces of length t single tab)\n\ -t (replaces tabs sequences of t spaces\n\ -t<integer> (specifies space-to-tab ration, t. default = 4)\n\ +v (changes spaces, tabs, , newlines visible characters\n\ -v (undoes actions of +v)\n\ -help (prints out help text)\n\ +t , -t incompatible\n\ +v , -v incompatible\n\n\ < (defines input program)\n\ > (defines output file)\n\ come in in next form:\n\ python3 program.py +v +t -t4 < input.py > temp.out") #end of help function def main(): # these 2 variables exist checks create sure user not phone call # +t , -t or +v , -v in same set of arguments. t_called = false v_called = false string = file_generator() print(string) first_arg = true arg in sys.argv: if first_arg: #the first argument = programme name first_arg = false elif (arg == "-t" , t_called == false): print("-t") print(space_to_tab()) t_called = true elif (arg == "+t" , t_called == false): print("+t") print(tab_to_space()) t_called = true elif (arg == "+v" , v_called == false): print("+v") print(string_to_ascii()) v_called = true elif (arg == "-v" , v_called == false): print("-v") print(ascii_to_string()) v_called = true elif (arg == "-help"): help_function() else: print("unrecognized argument") help_function() #end of main function main()
your immediate problem appears this:
input_string = get_input() if(get_input == eof): break
instead of testing string returned get_input()
—that is, input_string
—you're testing get_input
function. since function never equal character, true, you're stuck in loop forever.
but meanwhile, although insist get_input
part of assignment , can't changed, it's broken. we'll ignore fact it's bizarre design in first place. happens when eof?
try: ret = input() except eoferror: ret = ret+eof homecoming ret
that ret = ret+eof
trying access name ret
. nothing's been bound ret
; place have done ret = input()
, never made assignment, because of exception thrown input
. so, you're going unboundlocalerror: local variable 'ret' referenced before assignment
.
python loops replace tabs
Comments
Post a Comment