Python Forum

Full Version: How to make for loop display on 1 Line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have this auto typing text animation code

        response = "Hello there..."
        print ("Baxter: ")
        for i in response:
            sys.stdout.write(i)
            sys.stdout.flush()
            time.sleep(0.2)
This is what it outputs (But the "Hello there..." looks like it's being typed on the screen):
Output:
Baxter: Hello there...
I want it to be (Where "Baxter:" just appears and "Hello there... looks like it's being auto typed"):
Output:
Baxter: Hello there...
Is it possible to do this? If so, how?

Thanks in advance.



That's probably a bad example, seeing as I can just do
response = "Baxter: Hello there..."
I'm looking for a solution for the more complex one like this:
    if ('weather' not in command):
        if ('who is' in command) or ('what is the' in command) or ('what is a' in command):
            if ('time' not in command):
                speak('Searching Wikipedia...')
                command = command.replace("who is","")
                command = command.replace("what is the","")
                command = command.replace("what is a","")
                results = wikipedia.summary(command, sentences = 2)
                #print("Baxter:",results)
                #----------------------
                #Auto typing animation:
                print("Baxter: ")
                for i in results:
                    sys.stdout.write(i)
                    sys.stdout.flush()
                    time.sleep(0.05)
                print("\n")
                #----------------------
                return speak(results) 
The results print like this though:
Output:
Baxter: Blah blah blah Wikipedia info...
All I want is:
Output:
Baxter: Blah blah blah Wikipedia info...
I don't know what results contain but seeing that you're looping might could use a join
results = ['Blah', 'blah', 'blah', 'Wikipedia', 'info']
text = f'Baxter: {" ". join(results)}'
print(text)
Output:
Baxter: Blah blah blah Wikipedia info
Or, just tell print() to not include the newline.

import sys
import time

response = "Hello there..."
print ("Baxter: ", end="")
for i in response:
    sys.stdout.write(i)
    sys.stdout.flush()
    time.sleep(0.2)
(Jan-12-2022, 08:55 PM)bowlofred Wrote: [ -> ]Or, just tell print() to not include the newline.

import sys
import time

response = "Hello there..."
print ("Baxter: ", end="")
for i in response:
    sys.stdout.write(i)
    sys.stdout.flush()
    time.sleep(0.2)

Thanks!
The
end=""
solved it.