简体   繁体   中英

Can't get a function to work

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?

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 line def 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 eg [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

您应该将其称为exit() ,而不是exit

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM