2

I can't get the characters to all be printed on one line.

list = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for item in list:
  sleep(0.25)
  print(item)

i tried using a list and imported the sleep library

Michael M.
  • 10,486
  • 9
  • 18
  • 34
stowy_07
  • 31
  • 2
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow"). This site is not a code-writing or tutoring service, it is best used when you have a specific problem with written code. Your question, reads like "Can someone code this for me" or "Show me a tutorial or information on how to code this" which are off-topic for Stackoverflow. Please take the [tour](https://stackoverflow.com/tour), read [what's on-topic here](https://stackoverflow.com/help/on-topic). – itprorh66 Mar 18 '23 at 16:23
  • If any of the answers below solved your issue, then you should mark the one that best helped you as correct. This would help guide others with the same issue in the future. – Michael M. Mar 19 '23 at 15:30

4 Answers4

1

By default, print() ends each string with a newline character, but you can change this by providing your own ending string with the end= keyword argument. In your case, you'll want to pass end='' to print nothing at the end.

Also, by default, your terminal won't show any text unless there's a new line because of line buffering. To fix this, flush the output with flush=True

from time import sleep

lst = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for item in lst:
    sleep(0.25)
    print(item, end='', flush=True)
Michael M.
  • 10,486
  • 9
  • 18
  • 34
1

Did you know that you can iterate over strings? That means you don't need a list of chars:

import time

message = "Welcome to WORDLE"
for letter in message:
    print(letter, end="", flush=True)
    time.sleep(0.25)
print()
  • The end="" parameter tells print to place an empty string at the end, instead of the default '\n'
  • The flush=True parameter tells print to output the character right away instead of waiting for the internal buffer to get full before outputing.
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
0

In your print, add this code after item as a second parameter : end=''. By default, every time you printed, the default value \n was added.

  • Nothing will be printed because of line buffering. You'll need to either turn off line buffering or flush the output each time. – Michael M. Mar 18 '23 at 16:27
0

The print function takes an end argument, by default a newline. Change that to an empty string to get all letters on one line. You also need to add flush=True so that Python doesn't buffer the output till the line is done.

from time import sleep

letters = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for letter in letters:
  sleep(0.25)
  print(letter, end='', flush=True)

As pointed out by @Michael M., it's bad practice to name your variable e.g., "list", as this hides / shadows the official list keyword. I've renamed it to "letters" here, which makes it clearer what it is used for.

Robert
  • 7,394
  • 40
  • 45
  • 64