I am having issues importing time and using time.sleep.
I have students that will need to use this function, but it doesn’t seem to work on VSCode for Education. Are there any tips?
Great question @jerhieb! I struggled with this too. You should be able to import the time library in your code with import time
similar to how we do with our Spooky Manor game:
The standard output (print()
) is buffered differently in vscode.dev. This means that print
statements may not immediately flush their output to the terminal, and it seems like the program is not pausing when calling time.sleep()
directly. To get around the sheer speed of vscode.dev, as an example, I wanted to create a typewriter function, but it wasn’t actually displaying as typing, instead just streaming text.
So, I created a function called print_slow
(you can check out a copy here https://vscodeedu.com/S7CgM1HvnJJkHNzCZgpq:
def print_slow(str):
"""
Slows down the return of text to appear like a typewriter.
"""
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.005)
Here’s an example of how that is used in the Spooky Manor game:
print_slow("Oh no! You ran home.\n\nWhen you return back in the morning, you find that the manor has disappeared! \nIn it's place is a pile of rubble and a sign that says 'Coming Soon: Apartments!'\n\nBetter luck next time!")
Thanks for the response. However, when I try to use
import time
print(‘hi’)
time.sleep(1)
print(‘bye’)
hi and bye are printed simultaneously. Am I doing something wrong?
Also, I tried typing in exactly what your suggestion for the function print_slowly. When I called the function with a string parameter. The string was immeadiately displayed without any pause between letters.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.