Why are there spaces between the letters in my output?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tharden3
    Contributor
    • Jul 2008
    • 916

    Why are there spaces between the letters in my output?

    Below I am disclosing a script I wrote. I called it GhostWriter. It is supposed to write a message you type in as if a ghost were writing it for you. It even has some very basic AI to simulate a real person typing. The problem is, is that when it is run in the python shell, it puts spaces in between the letters of my message. Please submit a version of this that will take the spaces out of the Message being written by the ghost. (And yes, I am new to computer programming... so don't criticize my lame program!)


    Code:
    #--------------------------------------------------------------------------------------------
    from time import sleep
    from GhostWriter import *
    import random
    blist=[.1,.3,.3,.7,.05,.1,.3,.57,.7,.6,.6,.6,.5,.05,.5,.02,.05,.06,.03,.04,.05,.05,.05,.05,.06,.04,.05,.04]
    b=blist[0]
    def GhostAuthor(name,date,time,prompt):
        print "Message from: ",name
        print date
        print time
        for x in prompt[:]:
            loop=1
            while loop==1:
                random.shuffle(blist)
                sleep(blist[0])
                loop=0
            print x,
      
    print "Welcome to Ghost Writer. We are going to write a \"Ghost Message\""
    print "Please fill in the information and then click 'ENTER' to proceed."
    name=raw_input("What is your name?: ")
    date=raw_input("What is today's date?: ")
    time=raw_input("What time is it?: ")
    prompt=raw_input("Please type in your message: ")
    
    GhostAuthor(name,date,time,prompt)
    print " "
    print " "
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Replace the last line in your for loop ( print x, )
    with:
    sys.stdout.prin t( x )

    * And don't forget to import sys ;)

    Pretty neat program you came up with... impractical but neat to see.

    Comment

    • tharden3
      Contributor
      • Jul 2008
      • 916

      #3
      Originally posted by jlm699
      Replace the last line in your for loop ( print x, )
      with:
      sys.stdout.prin t( x )

      * And don't forget to import sys ;)

      Pretty neat program you came up with... impractical but neat to see.
      Thanks so much for the help. I'm still learning the ropes as far as python goes, so it's helpful to have someone who knows what they're doing.

      Comment

      • tharden3
        Contributor
        • Jul 2008
        • 916

        #4
        Originally posted by jlm699
        Replace the last line in your for loop ( print x, )
        with:
        sys.stdout.prin t( x )

        * And don't forget to import sys ;)

        Pretty neat program you came up with... impractical but neat to see.
        ehh, sorry to bug you again, but when I place sys.stdout.prin t(x) it comes up with invalid syntax. If anything, send me the code you added as code in the program, so I can see how it fits in there. Thanks

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Yeah, the docstring for sys.stdout is identical to that of open. You sure you aren't mixing in some Java/C# here by accident?

          Comment

          • tharden3
            Contributor
            • Jul 2008
            • 916

            #6
            Originally posted by Laharl
            Yeah, the docstring for sys.stdout is identical to that of open. You sure you aren't mixing in some Java/C# here by accident?
            I agree. would you know how to fix the problem? (In python of course)

            Comment

            • tharden3
              Contributor
              • Jul 2008
              • 916

              #7
              It's fixed. Thank you guys for the help.

              Comment

              • jlm699
                Contributor
                • Jul 2007
                • 314

                #8
                Originally posted by tharden3
                It's fixed. Thank you guys for the help.
                For completeness, could you post what modifications you made to make it work?

                My code below:
                [code=python]
                from time import sleep
                import random, sys
                blist=[ .1,.3,.3,.7,.05 ,.1,.3,.57,.7,. 6,.6,.6,.5,.05, .5,.02,.05,.06, .03,.04,
                .05,.05,.05,.05 ,.06,.04,.05,.0 4]
                b=blist[0]

                def GhostAuthor(nam e,date,time,pro mpt):
                print "Message from: ",name
                print date
                print time
                for x in prompt[:]:
                loop=1
                while loop==1:
                random.shuffle( blist)
                sleep(blist[0])
                loop=0
                sys.stdout.writ e( x )

                print "Welcome to Ghost Writer. We are going to write a \"Ghost Message\""
                print "Please fill in the information and then click 'ENTER' to proceed."
                name=raw_input( "What is your name?: ")
                date=raw_input( "What is today's date?: ")
                time=raw_input( "What time is it?: ")
                prompt=raw_inpu t("Please type in your message: ")

                GhostAuthor(nam e,date,time,pro mpt)
                print " "
                print " "

                """ Console output:
                Welcome to Ghost Writer. We are going to write a "Ghost Message"
                Please fill in the information and then click 'ENTER' to proceed.
                What is your name?: Jimmy
                What is today's date?: July 10, 2008
                What time is it?: 8:20 AM
                Please type in your message: This message is for ghost writing.
                Message from: Jimmy
                July 10, 2008
                8:20 AM
                This message is for ghost writing.
                """[/code]

                Comment

                • tharden3
                  Contributor
                  • Jul 2008
                  • 916

                  #9
                  Here's the modified verion:


                  Code:
                  from time import sleep
                  from GhostWriter import *
                  import sys
                  import random
                  blist=[.1,.3,.3,.7,.05,.1,.3,.57,.7,.6,.6,.6,.5,.05,.5,.02,.05,.06,.03,.04,.05,.05,.05,.05,.06,.04,.05,.04,.03,.04,.03,.04,.03,.04,.04,.035,.045,.05,.03,.03,.04,.04,.4,.6,.03,.05,.05,.04,.04]
                  b=blist[0]
                  def GhostAuthor(name,date,time,prompt):
                      print "Message from: ",name
                      print date
                      print time
                      for x in prompt[:]:
                          loop=1
                          while loop==1:
                              random.shuffle(blist)
                              sleep(blist[0])
                              loop=0
                          sys.stdout.write(x)
                  
                  print "Welcome to Ghost Writer. We are going to write a \"Ghost Message\""
                  print "Please fill in the information and then click 'ENTER' to proceed."
                  name=raw_input("What is your name?: ")
                  date=raw_input("What is today's date?: ")
                  time=raw_input("What time is it?: ")
                  prompt=raw_input("Please type in your message: ")
                  
                  GhostAuthor(name,date,time,prompt)
                  print " "
                  print " "
                  Thanks for the help.

                  Comment

                  • jlm699
                    Contributor
                    • Jul 2007
                    • 314

                    #10
                    Oh! An important note that I forgot to mention is that on Linux systems it may be necessary to call sys.stdout.flus h() after every write or else all text will appear at the end of the loop.

                    Comment

                    • tharden3
                      Contributor
                      • Jul 2008
                      • 916

                      #11
                      Originally posted by jlm699
                      Oh! An important note that I forgot to mention is that on Linux systems it may be necessary to call sys.stdout.flus h() after every write or else all text will appear at the end of the loop.
                      ooh, good point. I do run a linux distro on my PC.

                      Comment

                      • jlm699
                        Contributor
                        • Jul 2007
                        • 314

                        #12
                        It's probably a good idea to always call flush after a sys.stdout.writ e() call just for safety's sake.. My Windows box doesn't need it (My Red Hat distro does, that's how I caught it) but I wouldn't be surprised to see some that do.

                        Comment

                        Working...