Python Forum
How to make for loop display on 1 Line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make for loop display on 1 Line
#1
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...
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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)
Pedroski55 and BashBedlam like this post
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 45,698 May-06-2023, 08:14 AM
Last Post: pramod08728
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,574 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Skipping line in text without Restarting Loop IdMineThat 4 1,433 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  How to prevent python from going to new line in for loop? idknuttin 3 4,835 Feb-11-2022, 05:40 AM
Last Post: deanhystad
  Looking for discord bot to make loop ping for address ip tinkode 0 1,788 Jul-26-2021, 03:51 PM
Last Post: tinkode
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,270 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,172 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  For loop to make it easier Amirdst 2 2,026 Apr-28-2020, 04:12 PM
Last Post: Yoriz
  How to make input goto a different line mxl671 2 2,422 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  How to Display Multiple Time Tables With While Loop ZQ12 2 2,116 Nov-10-2019, 04:15 AM
Last Post: ZQ12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020