Ask coding questions

← Back to all posts
Typing Effect in Python
GangstaPichu (5)

I did this and the def line for some reason comes up as syntax error. I even followed a guide to the T on this matter. I don't get what's wrong with it?

import os,sys,time,sleep

black = "\033[0;30m"
red = "\033[0;31m"
green = "\033[0;32m"
yellow = "\033[0;33m"
blue = "\033[0;34m"
magenta = "\033[0;35m"
cyan = "\033[0;36m"
white = "\033[0;37m"
bright_black = "\033[0;90m"
bright_red = "\033[0;91m"
bright_green = "\033[0;92m"
bright_yellow = "\033[0;93m"
bright_blue = "\033[0;94m"
bright_magenta = "\033[0;95m"
bright_cyan = "\033[0;96m"
bright_white = "\033[0;97m"

time = 0.5

def ans("Do you want to play?")
for character:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(time)
print()
time.sleep(1)
input().lower()

Answered by Coder100 (18146) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18146)

Hi! The scrolling text is a function. You call it, but don't do:

def ans("Do you want to play?"):
  for character in ans:
    sys.stdout.write(character) 
    sys.stdout.flush()
    time.sleep(time)

that makes no sense.

Anyways, I think you mean:

def type(ans, time):
  for character in ans:
    sys.stdout.write(character) 
    sys.stdout.flush()
    time.sleep(time)

type("Do you want to play? ", 0.2);
yesorno = input().lower() # now do whatever you want with the input
GangstaPichu (5)

@Coder100 Traceback (most recent call last):
File "main.py", line 29, in <module>
type("Do you want to play? ", 0.2);
File "main.py", line 27, in type
time.sleep(time)
AttributeError: 'float' object has no attribute 'sleep'

That's what I get when I do that though?

Coder100 (18146)

try this:

def type(ans, delay):
  for character in ans:
    sys.stdout.write(character) 
    sys.stdout.flush()
    time.sleep(delay)

type("Do you want to play? ", 0.2);
yesorno = input().lower() # now do whatever you want with the input

@GangstaPichu

GangstaPichu (5)

@Coder100 Yeah, it works now. Lol

Now I just need to try and fiddle with it so the whole thing works now. It's being a bit tricky though. Thanks for the help!

Bookie0 (6272)

Hey!

on line 24 i think you forgot the colon : at the end. The the rest of the lines have to be indented.

But also if you want the typewriter effect, check out my tutorial here:
https://repl.it/talk/learn/Add-ons-to-your-Python-code-to-make-it-better/35792

basically you do this:

import sys, os, curses, time
from time import sleep

st = 0.04
def sp(str):
  for letter in str:
    sys.stdout.write(letter)
    sys.stdout.flush()
    time.sleep(st)
  print()

and st is the variable for how fast.

so to write a sentence you do:

sp("Hello")
# (sp stands for 'slowPrint')

and replace Hello with the text of your choice!

Happy coding and hope this helps! =)

GangstaPichu (5)

@Bookie0 Even with the colon it says it's invalid. Also, that's where I got this from mostly. Before that I looked at other things and stuff people said. Those didn't help out as much as that did.

Bookie0 (6272)

hum well remove what you put and use my version it should work! ;) @GangstaPichu