Hello, I'm remaking a game in python and i was wondering if there is a way i could shorten some of my code, here it is:
import time def displayIntro(): print("This is the story of a man named Stanley.") time.sleep(2) print("Stanley worked for a company in a big building where he was employee # 427.") time.sleep(2) print("Employee # 427’s job was simple: he sat at his desk in room 427 and he pushed buttons on a keyboard.") time.sleep(4) print("Orders came to him through a monitor on his desk, telling him what buttons to push, how long to push them, and in what order.") time.sleep(5) print("This is what employee 427 did every day of every month of every year, and although others might have considered it soul rending, Stanley relished every moment that the orders came in, as though he had been made exactly for this job.") time.sleep(10) print("And Stanley was happy.") print() print() time.sleep(4) print("And then one day, something very peculiar happened.") time.sleep(2) print("Something that would forever change Stanley;") time.sleep(2) print("something he would never quite forget.") time.sleep(3) print("He had been at his desk for nearly an hour when he realized that not one single order had arrived on the monitor for him to follow.") time.sleep(6) print("No one had shown up to give him instructions, call a meeting, or even say 'hi.' Never in all his years at the company had this happened, this complete isolation.") time.sleep(7) print("Something was very clearly wrong. Shocked, frozen solid, Stanley found himself unable to move for the longest time.") time.sleep(3) print("But as he came to his wits and regained his senses, he got up from his desk and stepped out of his office.") displayIntro()
as you can see it is quite lengthy im really new to python so if this is normal please let me know
You could do it with a 2D list, like this:
# this is a 2 dimensional list - a list of lists # it is in the form [ [sentence,time], [sentence,time], [sentence, time]] e.t.c intro = [["This is the story of a man named Stanley.",2],["Stanley worked for a company in a big building where he was employee # 427.",2],] # e.t.c. # for each sentence in intro (for each list in the large list) for sentence in intro: # print the first item print(sentence) # wait for the length of the second item time.sleep(sentence)
Please upvote if this helps :)
Since the timings are irregular and all, you can try putting the text and times in seperate lists and then call them with a loop
@AidanSlyker oh sorry.
Basically, what I mean is put all your text in a list of strings(I hope you know what they are).like
l=["This is the story of a man named Stanley.","Stanley worked for a company in a big building where he was employee # 427."...]
and a list of times like:
And then use a loop like
for i in range(len(l)): print(l[i]) time.sleep(t[i])
upvote if this helps!
You could make this much more readable.
import time script = """This is the story of a man named Stanley. Stanley worked... etc etc. ... office.""" for line in script.split('\n'): print(line) time.sleep(len(line)/100) # replace 100 with any number you want