Skip to content
← Back to Community
Typing Effect in Python
Profile icon
GangstaPichu

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 [earned 5 cycles]
View Answer
Voters
Profile icon
LandonSanders
Profile icon
NyeFromWestKaai
Profile icon
GangstaPichu
Comments
hotnewtop
Profile icon
Coder100

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
Profile icon
GangstaPichu

@Coder100 Traceback (most recent call last):
File "main.py", line 29, in
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?

Profile icon
Coder100

oops wrote it wrong @GangstaPichu

Profile icon
Coder100

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

Profile icon
GangstaPichu

@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!

Profile icon
Coder100

lol ok np! gl! @GangstaPichu

Profile icon
Bookie0

Hey!

Screen Shot 2020-07-26 at 2.18.07 PM

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! =)

Profile icon
GangstaPichu

@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.

Profile icon
Bookie0

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