0

I am try to create a function that will end my game with a message counting down until it ends, and I am having to repeat this block of code a lot in my text adventure game, so I decided to make a function of it for the sake of neatness, and efficiency. But I cannot figure out how to define and call such a function. This is the code I am trying to execute:

print "That\'s a real shame..."
time.sleep(1)
print 'Exiting program in 5 seconds:'
time.sleep(1)
print '5'
time.sleep(1)
print '4'
time.sleep(1)
print '3'
time.sleep(1)
print '2'
time.sleep(1)
print '1'
time.sleep(1)
sys.exit('Exiting Game...')
break

So I defining the function like this:

def exit():
    print "That\'s a real shame..."
    time.sleep(1)
    print 'Exiting program in 5 seconds:'
    time.sleep(1)
    print '5'
    time.sleep(1)
    print '4'
    time.sleep(1)
    print '3'
    time.sleep(1)
    print '2'
    time.sleep(1)
    print '1'
    time.sleep(1)
    sys.exit('Exiting Game...')
    break

And I am calling the function like this:

elif ready == 'n':
    exit

What am I doing wrong?

eandersson
  • 25,781
  • 8
  • 89
  • 110
Ryan Hosford
  • 519
  • 1
  • 9
  • 25

4 Answers4

7

You would call the function by typing exit(). I modified your countdown code and turned it into a function that I called inside exit() to demonstrate how to call one function from piece of code.

def exit():
    print "That\'s a real shame..."
    time.sleep(1)
    print 'Exiting program in 5 seconds:'
    time.sleep(1)
    count_down(5) # Call Countdown clock
    print 'Exiting Game...'
    sys.exit()

def count_down(number):
    for i in reversed(range(number)):
        print i+1
        time.sleep(1)

exit() # <-- This how you call exit, you were missing the parentheses at the end.

Output:

That's a real shame...
Exiting program in 5 seconds:
5
4
3
2
1
Exiting Game...

Edit: Added more in-depth explanation.

The first linedef count_down is a function that takes one parameter and has one purpose, to handle the count down.

def count_down(number):

The second row contains what we call a for loop. The purpose of this code is to loop through objects. Starting from 4 then 3,2,1 etc. And the variable i at the same row will change for each time the loop goes through a number and is accessible only inside the loop. The first time print is executed it will be 5, then the next time 4 and so on.

 for i in reversed(range(number)):

In this function we also use two additional keywords and one parameter, reversed, range and the parameter number.

reversed(range(number))

range is used to create a list of numbers e.g. [0, 1, 2, 3, 4], that the for statement will loop through starting with 0, then it takes the next number all the way until it reaches the last number 4. I will explain why it starts at zero and only goes to four, and not five at the end of my answer.

reversed is used to reverse the list we created with range. Since we want to start at 4, and not 0.

Before reversed => [0,1,2,3,4]

After reversed] => [4,3,2,1,0]

number is a parameter. A parameter is a value that we provide when we execute the function from your exit() function by including a value inside the parentheses (). In this case we specified 5, so the list we created with range will range from 0 - 4 (0,1,2,3,4 = five numbers in total). If you instead specified 10 within the parentheses it would create a list starting from 0 all the way to 9. And your code would count down from 10 to 1, instead of from 5 to 1.

When Python has started working on the for loop it will execute the code inside, starting with print and then sleep, and continues to do so for for each number in the list created by range. Since we specified five in this case, it will execute the code a total of five times.

As Python is executing the code within the for loop it will first call the print function. Because the for loop will start at 4, not 5, we need to do some basic arithmetics and increase the value of each item we loop through by one. We do this by typing + 1 after our variable i.

The reason why it starts at 4 and not 5 is because in programming lists starts with the number 0, and not 1. There is a more technical explanation available on the reason why lists start with 0, and not 1 (or 4, and not 5 in this case since we reversed the list) here

Community
  • 1
  • 1
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • Thank you so much for your help. I am very new to python, and programming in general, but could you attempt to explain the count_down function to me? I don't quite understand for i in reversed(xrange(number)): – Ryan Hosford Apr 17 '13 at 23:11
  • Anything in particular that you need explaining? – eandersson Apr 17 '13 at 23:12
  • Sure. Give me a few minutes. – eandersson Apr 17 '13 at 23:13
  • @RyanHosford I tried to keep it as basic as possible. Feel free to poke me again if you have any questions. – eandersson Apr 17 '13 at 23:52
  • Thanks a lot for explaining that to me, it makes sense to me now – Ryan Hosford Apr 18 '13 at 00:51
  • Sorry to bother you with one last thing, but how would i include a break statement in a function? I am trying to make another function with an if/elif inside it, and i want the if to have a break in it so it breaks out of a while loop. I hope that made sense. Sorry if it didn't – Ryan Hosford Apr 18 '13 at 01:35
  • Take a look at this http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops If that doesn't help open another question and I'll try to answer it when I wake up again tomorrow. :) – eandersson Apr 18 '13 at 01:36
4

You are supposed to call it as exit(), not exit.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
0

It is simple. Use a tuple that holds the message and the timer delay so you can even control the delay time for each message.

import time, sys
messages = [
    ("That's a shame", 1),
    ("Exiting program in 5 seconds", 1),
    (None, 5)
]

for k,v in messages:
    if k:
        print(k)
        time.sleep(v)
    else:
        # start countdown timer
        while v:
            print v
            time.sleep(1)
            v -= 1

sys.exit()
stun
  • 1,684
  • 3
  • 16
  • 25
-1

A good way to do this code is:

def exit():
    timer= 5
    print "That\'s a real shame..."
    time.sleep(1)
    print 'Exiting program in 5 seconds:'
    for i in range (5):
        time.sleep(1)
        print timer
        timer = timer - 1
    sys.exit('Exiting Game...')

##You call the function like this
exit()
Hayden
  • 229
  • 2
  • 5
  • 12