1

I'm new with very little experience programming in Python, correct me if my terminology is incorrect.

I've seen posts asking about the typing effect in Python. But I would also want to use that effect in scripts that require you to answer or type something, like those Choose-Your-Own-Adventure games. For example:

answer = input("You reach a crossroad, would you like to go left or right?").lower().strip()
if answer == 'left':
     answer = input('You encounter a monster, would you like to run or attack?')
elif answer == 'right':
     answer = input('You walk aimlessly to the right and fall on a patch of ice.')

How would I have something like this have a typing effect?

wowsocool
  • 13
  • 3
  • Do you mean that the user's input will have the effect (while the user types), or the prompt? If it's the latter, why didn't the link answer your question? – Tomerikoo Oct 04 '20 at 08:39

2 Answers2

1

You can define a function for a typing effect, something like this:

import sys
import time

def type_effect(string, delay):
    for char in string:
        time.sleep(delay)
        sys.stderr.write(char)

And then use it every time you want to use the effect :)

type_effect('You reach a crossroad, would you like to go left or right?', 0.1)
answer = input().lower().strip()
if answer == 'left':
    type_effect('You encounter a monster, would you like to run or attack?', 0.1)
    answer = input()
elif answer == 'right':
    type_effect('You walk aimlessly to the right and fall on a patch of ice.', 0.1)
    answer = input()

Or, you can even define a function that uses the type effect and also returns the user's input, like this:

import sys
import time

def type_effect_and_input(string, speed):
    for char in string:
        time.sleep(speed)
        sys.stderr.write(char)
    return input().lower().strip()

answer = type_effect_and_input('You reach a crossroad, would you like to go left or right?', 0.1)
if answer == 'left':
    answer = type_effect_and_input('You encounter a monster, would you like to run or attack?', 0.1)
elif answer == 'right':
    answer = type_effect_and_input('You walk aimlessly to the right and fall on a patch of ice.', 0.1)
pitamer
  • 905
  • 3
  • 15
  • 27
  • I need some more info. How can I use it every time I want to use the effect? How would I use it in the context of the example I put in my post? – wowsocool Aug 11 '20 at 18:07
  • Oh I see now, you want to get input and use a typing effect at the same time! OK, updated the answer to include two ways of achieving this :) @wowsocool – pitamer Aug 11 '20 at 18:21
0

Here's how you do it. I am gonna include in a module called textutils from time import sleep

def slow_print(text, waitTime=0.05):
    for x in text:
        print(x, end='')
        sleep(waitTime)

slow_print("You reach a crossroad, would you like to go left or
right?")
answer = input("").lower().strip()
if answer == 'left':
     slow_print('You encounter a monster, would you like to run or attack?')
     answer = input('')
elif answer == 'right':
     slow_print('You walk aimlessly to the right and fall on a patch of ice.')
     answer = input()
lowhatwo
  • 31
  • 1
  • 5