I am trying to create a delay based on the length of a string. But the delay has to happen before the string is printed in the console. The goal is to add a bit of realism to an adventure game.
This is the current code I have come up with. I know it isn't right, but this is the first week I've been using Python.
import time
def calculate_text_speed(delay):
delay = text.len() / 20
def start_game():
calculate_text_speed(delay)
time.sleep(calculate_text_speed.delay)
print("Test message.")
Should I create a list with dialogues to calculate the the strings beforehand? This will probably decrease the readability of the code, right?
Perhaps this is what you mean by not declaring text?
def print_with_delay(text):
time.sleep(len(text) / 20)
print(text)
print_with_delay("Some very exciting things are happening....")
Original answer: You can create very readable code without generating the text list ahead of time.
The main issue is you are passing the wrong thing to calculate_text_speed. Try this.
import time
text = "Some very exciting things are happening...."
#pass text to the calculate_text_speed function
def calculate_text_speed(text):
return len(text) / 20
time.sleep(calculate_text_speed(text))
print(text)