# Using time.sleep

**URL:** <https://community.vscodeedu.com/t/using-time-sleep/990>\
**Category:** Introduction to Python\
**Created:** [September 12, 2024, 2:30pm UTC](https://community.vscodeedu.com/t/using-time-sleep/990 "2024-09-12T14:30:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerhieb](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jerhieb](https://community.vscodeedu.com/u/jerhieb)\
**Post date:** [September 12, 2024, 2:30pm UTC](https://community.vscodeedu.com/t/using-time-sleep/990/1 "2024-09-12T14:30:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adamsmeets](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.vscodeedu.com/adamsmeets/32/286_2.png) [@adamsmeets](https://community.vscodeedu.com/u/adamsmeets)\
**Post date:** [September 13, 2024, 2:31pm UTC](https://community.vscodeedu.com/t/using-time-sleep/990/2 "2024-09-13T14:31:06Z")

</div>

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:

![image](https://canada1.discourse-cdn.com/flex035/uploads/msft_cs_plc/original/1X/9029fb682fa8a087480cb769ca4609a580e4f9f2.png)

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:](https://vscodeedu.com/S7CgM1HvnJJkHNzCZgpq:)

```python
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:

```python
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!")

```

---

<div class="post-metadata">

**Author:** ![jerhieb](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jerhieb](https://community.vscodeedu.com/u/jerhieb)\
**Post date:** [September 13, 2024, 3:26pm UTC](https://community.vscodeedu.com/t/using-time-sleep/990/3 "2024-09-13T15:26:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jerhieb](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jerhieb](https://community.vscodeedu.com/u/jerhieb)\
**Post date:** [September 13, 2024, 6:56pm UTC](https://community.vscodeedu.com/t/using-time-sleep/990/4 "2024-09-13T18:56:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.vscodeedu.com/system/32/67_2.png) [@system](https://community.vscodeedu.com/u/system)\
**Post date:** [September 14, 2024, 6:56pm UTC](https://community.vscodeedu.com/t/using-time-sleep/990/5 "2024-09-14T18:56:52Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
