How to make for loop display on 1 Line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to make for loop display on 1 Line (/thread-36050.html) |
How to make for loop display on 1 Line - Extra - Jan-12-2022 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): I want it to be (Where "Baxter:" just appears and "Hello there... looks like it's being auto typed"): 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: All I want is:
RE: How to make for loop display on 1 Line - menator01 - Jan-12-2022 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)
RE: How to make for loop display on 1 Line - bowlofred - Jan-12-2022 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) RE: How to make for loop display on 1 Line - Extra - Jan-12-2022 (Jan-12-2022, 08:55 PM)bowlofred Wrote: Or, just tell print() to not include the newline. Thanks! The end=""solved it. |