How do i write a string letter by letter using concatenation in python
Question:
I currently have a way to write a string letter by letter but when i try and include more arguments it gives me the error that it was expecting 1 and not 3
I used this to define
def delay_print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.25)
i then tried this code:
delay_print("You have" ,lives, "lives left")
and then go the error:
delay_print("You have" ,lives, "lives left")
TypeError: delay_print() takes 1 positional argument but 3 were given
Answers:
Thats because you pass 3 arguments. The comma separates the arguments a function recieves. what you can do is
delay_print("You have" + lives + "lives left")
or more fancy with the f-string syntax.
delay_print(f"You have {lives} lives left")
You could use a variable number of arguments like this:
import sys
import time
def delay_print(*args):
for s in args:
for c in str(s):
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.25)
delay_print("You have ", 2, " lives left")
Output:
You have 2 lives left
I currently have a way to write a string letter by letter but when i try and include more arguments it gives me the error that it was expecting 1 and not 3
I used this to define
def delay_print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.25)
i then tried this code:
delay_print("You have" ,lives, "lives left")
and then go the error:
delay_print("You have" ,lives, "lives left")
TypeError: delay_print() takes 1 positional argument but 3 were given
Thats because you pass 3 arguments. The comma separates the arguments a function recieves. what you can do is
delay_print("You have" + lives + "lives left")
or more fancy with the f-string syntax.
delay_print(f"You have {lives} lives left")
You could use a variable number of arguments like this:
import sys
import time
def delay_print(*args):
for s in args:
for c in str(s):
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.25)
delay_print("You have ", 2, " lives left")
Output:
You have 2 lives left