-1

The question is about this project: https://repl.it/@LerconnDesign/First-Project

I wanted to write a code to print something slowly and not instantly. Basically it prints some part of the actual thing that is going to be printed and adds one letter every few milliseconds so the output would look like this if we have "welcome" as our actual word to print:

W
We
Wel
Welc
Welco
Welcom 
Welcome

I found the solution to that (I'll show the code) but I have another question from this.
So I used this code (in a new file not in the "main"):

from time import sleep
speed=input("Time Passes Between Characters (Please enter a real NUMBER):")
speed=float(speed)
state="NoError"
def print1(text, sleepy=speed):
  allprint=""
  for a in text:
        allprint+=a
        print(allprint, end="\r")
        sleep(sleepy)

Imported that into the main file and used it when needed. For example:

print1("Hello Lerconn User!\n")

It worked for a while. But then... Some of the texts I used this on started to copy themselves.

So the first part of the code that started doing this is like below:

print1("You have chosen the wait-times as below: \n between each character: " + str(speed) + " \n between each sentence: " + str(speed2))

So it should have probably printed this if speed and speed2 were both 1:

*You have chosen the wait-times as below: 
between each character: 1
between each sentence: 1*

However, it did something more like this:

You have chosen the wait-times as below: 
You have chosen the wait-times as below: 
You have chosen the wait-times as below: 
You have chosen the wait-times as below: 
You have chosen the wait-times as below: 

And keeps on copying. (It literally covers the entire screen so I only pasted a small part of the output (Nothing valuable in the remaining part , only the same copied so much times.)

If you want any other detail about the code, I will help you as much as I can.
If you didn't find this explanation enough, you can go to the link I gave at the beginning.

swiss_knight
  • 5,787
  • 8
  • 50
  • 92
Lerconn Design
  • 43
  • 1
  • 1
  • 8
  • 1
    It's probably not your code, but your display. Try resizing the window where the text is being printed and see what happens. – vanPelt2 May 22 '20 at 19:53
  • Btw there are some textes that automatically become bold even if I didn't make them bold (I don't know how to fix it) – Lerconn Design May 22 '20 at 19:54
  • SMcQ ok I'll try – Lerconn Design May 22 '20 at 19:54
  • Thanks for your reply but it didn't work (or I couldn't make it). I don't know it will be useful or not, but, if you have a repl.it account,maybe I can give you the control of the project (by your username or by a link) – Lerconn Design May 22 '20 at 19:57

1 Answers1

0

One of the problems is the newline character, you can modify your print1 function to include the newline if block.

def print1(text, sleepy=speed):
  allprint=""
  for a in text:
    if a == "\n":
      allprint = ""
    allprint+=a
    print(allprint, end="\r")
    sleep(sleepy)

If you do this and split your single print1 function call into multiple calls, based on the newlines as follows, then it works as desired.

print1(f"You have chosen the wait-times as below:")
print1(f"  between each character: {speed}")
print1(f"  between each sentence: {speed2}")
incarnadine
  • 658
  • 7
  • 19