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
CodePudding user response:
It seems like you should rename the variable str inside the function slow_print.
str is a reserved keyword in python and it should be the cause of your error.
I have tried this solution without issues:
import sys
import time
import random
def slow_print(t):
for letter in t:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(random.random() * 10.0 / 55)
print('')
if __name__ == '__main__':
slow_print("Welcome, this is a text-based adventure game.")
CodePudding user response:
slow_print takes text and slowly prints it. For it to work, you have to give it text.
intro does not return text. It prints text directly, then automatically returns None, the default return value for functions that don't explicitly return something else. slow_print then receives None and tries to slowly print it, but None is not text.
You need to restructure your code so the text that needs to be slowly printed is passed to slow_print instead of print. That might involve giving intro a parameter to select how to print things:
def intro(printer=print):
printer("Welcome, this is a text-based adventure game.")
printer("This isn't designed for the light hearted so proceed at your own peril.")
...
...
intro(slow_print)
In this modified code, intro has a printer parameter that it will use to print things. This parameter defaults to the built-in print function, but if you pass slow_print instead, intro will use slow_print to print its messages.
CodePudding user response:
slow_print(intro()) prints what intro() returns, and this function does not return anything. You can rewrite this function.
def intro():
rerurn '''Welcome, this is a text-based adventure game.
This isn't designed for the light hearted so proceed at your own peril.'''
Then your slow_print will print intro message.
P. S. If you use intro() elsewhere, you should replace intro()to print(intro()).
