Ask coding questions

← Back to all posts
Game In Python
AidanSlyker (13)

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

Answered by ArchieMaclean (751) [earned 5 cycles]
View Answer
Commentshotnewtop
ArchieMaclean (751)

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[0])
  # wait for the length of the second item
  time.sleep(sentence[1])

Please upvote if this helps :)

Murzikal (22)

lol stanley parable

Aidan72 (7)

@Murzikal Yes i love it, they are remaking it with more content

Murzikal (22)

@Aidan72 lol its a fun game, i think you made a good choice

Lord_Poseidon (114)

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 (13)

@Lord_Poseidon uh can you please elaborate this is literally my first time ever using anything other than the website building languages
(not meant to be passive aggressive)

Lord_Poseidon (114)

@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:t=[2,2,4,5,...]
And then use a loop like

for i in range(len(l)):
    print(l[i])
    time.sleep(t[i])

upvote if this helps!

AidanSlyker (13)

@Lord_Poseidon i think i see what you mean, thank you :)

ArchieMaclean (751)

@Lord_Poseidon sorry, I didn't see that you had answered :|

AidanSlyker (13)

@Lord_Poseidon would i put that all in

def displayIntro():

nvm, im doing it a different way

Lord_Poseidon (114)

@ArchieMaclean NVM. Your way was better.Why didn't I think of 2d arrays?

Aidan72 (7)

:facepalm: I HAVE BEEN USING THE WRONG ACCOUNT FOR 2 DAYS STRAIGHT WHAT LMAO

a5rocks (732)

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