#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Works good in idle but not in cmd


    hello,
    i'm absolutely a beginner in programming, just started out with Python3, i was trying to write a code so when doing the print function the text will show as if it's being typed in a character type manner,
    i made a simple code, and wrote a simple program that count how many times a string would show in a text, using the typing like manner to display text on screen.
    it worked like a charm when i ran it in idle, but when in terminal it's not.
    and i want to export the code as an executable, so the program would be running in a console thus losing the main purpose of the code, any suggestions,
    i dont know how to use the code button correctly,
    What the hell i cant find how to make an attachment!!
    And i cant post a URL
    ubuntuone/4cNIAycHCKbgyvuBZKKISK
    just put a .com after ubuntuone
    thank you
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481

    flush twice, kitchen is far away.


    Flushing the output gives the behavior I understand you to want. I inserted two lines,

    import sys
    and
    sys.stdout.flush()
    Code:
    #strings counter.
    import time
    import sys
    def typeWriter(text):#definig the function that would type write a string.
    
        i=int(len(text))-1#the -1 is because the characters positions starts at 0 and the len function returns a value starting by 1.
        b=0               #0 is the position of the first character.
        while b<i:        #loop till you reach the character tha's before the last one.
            if text[b]=='\n':#this is because you need more time to start a new line.
                time.sleep(0.4)
                print(text[b], end='')#printing the character with position b right
                b+=1                  #before the character with position b+1
            else:
                time.sleep(0.01)      #adjust the argument of the sleep function so it woulf feels natural to you.
                print(text[b], end='')
                sys.stdout.flush()
                b+=1
        time.sleep(0.01)
        print(text[b])#printing the last character of the string.
    
    typeWriter('This Simple Program Would Count Characters or Words or Sentences\noccurence in a text that you would provide.\n\nThis\
     Was Written By a Beginner Programmer For The Purpose Of Training.\n\nPRESS ENTER TO PROCEED.')
    input()
    
    option='yes'#asigning this before the loop asures intiating the loop for the first time.
    while option=='yes':
        typeWriter('Please type in your text, and press Enter only when you finish:')
        text=input()#asking for the text that the user want's to count characters occurence in it.
        option2='yes'#this is for the next loop.
        
        while option2=='yes':
            typeWriter('please type in the string of interest:')
            string=input()
            a=str(text.count(string))
            typeWriter(a)
            typeWriter('do you want to do the count for another string? (yes or no)')
            option2=input()
    
            while option2 !='yes' and option2 !='no':#this to insure that the program won't accept input other than 'yes' or 'no'
                typeWriter('please choose a valide option (yes or no)')
                option2=input()
            
        typeWriter('would you like to try another text? (yes or no)')
        option=input()
        
        while option !='yes' and option !='no':
            typeWriter('please choose a valid option (yes or no)')
            option=input()
            
    typeWriter('Adios amigo!\n \n ')
    time.sleep(3)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Flushing the output gives the behavior I understand you to want. I inserted two lines,

    import sys
    and
    sys.stdout.flush()
    THANX a lot ,, it worked!!!!

IMN logo majestic logo threadwatch logo seochat tools logo