-1

So, I'm making a slot machine program in Python (2.7.10) and I want to add a space of time between when the player types spin and when the results for the spin show on the screen. What I'd love to have is, for the player to spin, wait five seconds then get three results, coming in with three seconds between each one. Problem is, I have no idea how to add a timer! I need to know how to delay or allow time before their results from the spin show up.

oisinvg
  • 592
  • 2
  • 8
  • 21
  • Have you tried something? Please post what you have tried. – carloabelli Aug 29 '15 at 20:28
  • I actually have genuinely no idea how to use the timer module- I'm new to coding. So I've never attempted a code. – oisinvg Aug 29 '15 at 20:32
  • Before posting here please attempt to google your question and attempt a solution which you can come here with if it does not work. Also the way to learn to code is to write it yourself not ask people to write it for you – carloabelli Aug 29 '15 at 20:33
  • 1
    Your question seems to be a duplicate of this: http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python – Konstantin Schubert Aug 29 '15 at 20:36
  • oh yeah, apologies. I'll look around for longer next time. But thank you for your help. – oisinvg Aug 29 '15 at 20:41

2 Answers2

0

The time module from the standard library has a sleep function. To wait 3 seconds you can do:

import time # import the time module
time.sleep(3) # sleep for 3 seconds

When the sleep function is executed, the python script simply stops running for the time you give in the argument.

And by the way, the import time needs to be done only once. It is usually best if you put it at the top of your script. You will often want to import many modules, so it is good to have it all in one place.

Konstantin Schubert
  • 3,242
  • 1
  • 31
  • 46
  • used this method, but when i ran the code i think it caused the results of a spin to print (those are what I delayed). (I call the first function at the end of the code so I can define everything) – oisinvg Aug 29 '15 at 22:07
  • `import time` is not a function, it is a keyword with an argument. And you have to import at the beginning, not the end. I am not sure if I understand the rest of your comment. You need to call `time.sleep(3)` before the thing you want delayed. – Konstantin Schubert Aug 30 '15 at 07:49
  • what i meant is, in my code i write it all out before i call any functions, acknowledging those are not import time, but the code under def. The thing is as Python reads my code before getting to where I call the first function, it comes across time.sleep(3), and runs it, creating an early spin result. – oisinvg Aug 30 '15 at 10:45
0

You can always use the time library, and call the sleep function for the amount of seconds you want. EX:

import time
...
time.sleep(<amount of seconds>)
m_callens
  • 6,100
  • 8
  • 32
  • 54