[Tutor] BF Program hangs
Hello i made this program which interprets brainf*** But i don't understand why it doesn't endsand also it doesn't print anything import sys cell=[0] * 3 code_pointer=0 cell_pointer=0 close_brace_pos=0 open_brace_pos=0 bf=raw_input("Input bf program:") while (code_pointer0: code_pointer+=1 else: code_pointer=close_brace_pos+1 elif c==']': close_brace_pos=code_pointer code_pointer=open_brace_pos else: code_pointer+=1 For those of you who don't understand brainfuck its a very simple 8 instruction language http://en.wikipedia.org/wiki/Brainfuck ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] BF Program hangs
On Sat, Jun 18, 2011 at 10:02 PM, Kaustubh Pratap chand wrote: > Hello i made this program which interprets brainf*** > > But i don't understand why it doesn't endsand also it doesn't print > anything can you tell us more about the code other than "it doesn't end" and "it doesn't print anything?" *why* doesn't it print anything? do you have a print statement or function in your code? no, you don't. also, why do you use sys.stdout instead of print? we just want to know your rationale because most people would use print instead. also, why don't you think your program ends? what is preventing "the end" from happening? the more you can describe your problem as well as the code you've written, the easier it will be for us to help you out. please also document your code so that other beginners can read it too... thanks! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.chun : wescpy-gmail.com : @wescpy python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] BF Program hangs
On 01/-10/-28163 02:59 PM, Kaustubh Pratap chand wrote: > Hello i made this program which interprets brainf*** > > But i don't understand why it doesn't endsand also it doesn't print anything > > import sys > > cell=[0] * 3 > code_pointer=0 > cell_pointer=0 > close_brace_pos=0 > open_brace_pos=0 > > bf=raw_input("Input bf program:") > > while (code_pointer > c=bf[code_pointer] Indentation error. Presumably a flaw in your email program. Try using text mode. > > if c=='.': > sys.stdout.write(cell[cell_pointer]) > code_pointer+=1 write() doesn't take an integer parameter, but only character strings. If you want to be true to the original language, you'll need a chr() function there. But if I were you, I'd be debugging this by making it print something more verbose, in case the problem is invisible characters. > > elif c==',': > cell[cell_pointer]=sys.stdin.read(1); > code_pointer+=1 This one's a bit tougher, since I don't know any portable way to get a single character of input from a user. In any case, you'll be wanting to use ord() to convert (one of) user's character to an integer. > > elif c=='>': > cell_pointer+=1 > code_pointer+=1 > In any case, I'd make two other changes till you get it working: 1) make the input explicit, so people can know what you're testing this with. After all, if someone were to run it and just press enter, then of course it would print nothing. 2) add a print statement of some sort to the loop, so you can see how the pointers are doing. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor