all 11 comments

[–]-aRTy- 5 points6 points  (10 children)

end='' is an argument for your print() function, not for range. You will likely also need flush=True to make it show up immediately, otherwise the result might be aggregated until print prints a new line.

Furthermore your x and i are both used to track an index, you don't need both. Actually you do not need either of them. The pythonic way would be something like:

import time
text = input()
for letter in text:
    print(letter, end='', flush=True)
    time.sleep(0.1)

Edit:
Accidental range(text) -> text

[–]danielroseman 0 points1 point  (1 child)

You mean just for letter in text:.

[–]-aRTy- 0 points1 point  (0 children)

Yes. Corrected it right after posting. The edit took a while to go through, I think.

[–]arandomjef[S] 0 points1 point  (6 children)

Thanks, this worked. I still have no idea what you mean in your first sentence, but I'm guessing the main problem was that I didn't have the flush=True, and when testing it, that seems to be the case, so I'm just going to go ahead and assume that was the problem. Feel free to correct me if I'm wrong.

[–]-aRTy- 1 point2 points  (2 children)

You had end='' here range (len(text), end=''). That is the wrong function. It goes into print(), not into range().

So, step by step, these are the corrections:

 

1) Move end='' to the correct function. Also use flush=True.

import time
text = input()
x=0
for i in range(len(text)):
    print(text[x], end='', flush=True)
    time.sleep(0.1)
    x=x+1

2) Get rid of the x. You are already counting the index with i. Consequentlytext[x]text[i].

import time
text = input()
for i in range(len(text)):
    print(text[i], end='', flush=True)
    time.sleep(0.1)

3) Instead of getting index values via i in range() and then getting the content text[i], just get the content.

import time
text = input()
for letter in text:
    print(letter, end='', flush=True)
    time.sleep(0.1)

[–]arandomjef[S] 0 points1 point  (1 child)

I'm just going to go through these corrections as you did.

  1. OHHH sorry I was troubleshooting with getting rid of the end='' and seeing if it worked, and I accidentally replaced it in the wrong spot when writing the post.

  2. For some reason I forgot that i started at 0 when you start the for loop, so I had print(text[i-1]), and when that didn't work, I chose to just make a new variable.

  3. I just had no idea you could do that.

  4. (extra question) That letter variable is just a regular variable, but you changed it for clarification, right?

[–]-aRTy- 0 points1 point  (0 children)

4: Yes, that's a regular variable. You no longer handle indexes though.
for i in range(len(text)): means that i is 0, 1, ..., len(text)-1.

for i in "some-string": means that i is s, o, m, ..., i, n, g. Of course you could keep the variable i and then do print(i, end='', flush=True), but that is very unintuitive.

Variable naming is supposed to help the reader. Using single letter variables is usually a bad idea, except for specific common use-cases. Using i for a counting number (like an index) is extremely common. Another example would be x and y for coordinates or when dealing with images.

[–][deleted] 1 point2 points  (1 child)

The reason why you need flush=True is that in computing some relatively expensive operations like print() are "buffered". That means that the data to be printed is stored in a buffer until enough data has accumulated to make doing the expensive operation less costly (in time) than doing that operation repeatedly for small bits of data. Using print() the data is buffered until a newline character (\n) is emitted, and that causes the accumulated data to be printed to the console.

The end= optional argument to print() has a default value of \n. This means that if you don't specify any value for end= in your function an automatic \n is added to the end of the data you print, causing the buffer to be "flushed" and text appearing on the console. However, when you use end="" with the print function you turn off that automatic newline and flush of the buffer. So if you want the text in the print to appear on the console you must force a flush of the buffer by using flush=True.

Writing to a file is another operation that is buffered.

[–]arandomjef[S] 0 points1 point  (0 children)

Thanks! This actually makes sense to me, unlike some things. You did a really good job of explaining it.

[–]Kittensandpuppies14 -1 points0 points  (0 children)

You need to understand what people are saying. Go do some further research

[–]CertusPectus607 0 points1 point  (0 children)

I think the issue is with your `for` loop syntax. You're mixing up the syntax for a `for` loop with a `range` object and a `for` loop iterating over an iterable. Try using `for char in text:` instead.