vendredi 4 février 2022

Passing print function within another function

I decided I would create a simple text-based game as a learning project for Python as this is my first time using it. I have this function slow_print that simulates human typing which works in it's current form for strings, however is it possible to pass through a function instead. As I would like to break up the game text into sections for tidiness.

def slow_print(t):
    for letter in t:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(random.random()*10.0/55)
    print ('')
def intro():
    print("Welcome, this is a text-based adventure game.")
    print("This isn't designed for the light hearted so proceed at your own peril.")
    ...
    ...

Is this possible in Python? After playing around with the params and passing it through I got the error of "Python object is not iterableable". From my understanding strings would be iterateable. Would there be another easier way to accomplish this?

slow_print(intro())

ERROR:

Traceback (most recent call last):
  File "C:\Users\CDN Admin\Python\game.py", line 20, in <module>
    slow_print(intro())
  File "C:\Users\CDN Admin\Python\game.py", line 9, in slow_print
    for letter in t:
TypeError: 'NoneType' object is not iterable```



Aucun commentaire:

Enregistrer un commentaire