Unable to get function to work

I am trying to create a function that will end my game with a message counting to the end, and I will have to repeat this block of code many times in my text adventure game, so I decided to make this function for the sake of accuracy and efficiency. But I can’t understand how to define and name such a function. This is the code I'm trying to execute:

print "That\ 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 define the function as follows:

 def exit(): print "That\ 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 call the function as follows:

 elif ready == 'n': exit 

What am I doing wrong?

0
source share
4 answers

You can 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 a piece of code.

 def exit(): print "That\ 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 a real shame... Exiting program in 5 seconds: 5 4 3 2 1 Exiting Game... 

Edit: Added a more detailed explanation.

The first line of def count_down is a function that takes one parameter and has one purpose to handle the countdown.

 def count_down(number): 

The second line contains what we call for the loop . The purpose of this code is to cycle objects around. Starting with 4 , then 3,2,1 , etc. And the variable i in the same line will change every time the cycle goes through a number and is available only inside the cycle. It is executed the first time when 5 is executed, then the next time 4, etc.

  for i in reversed(range(number)): 

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

 reversed(range(number)) 

range is used to create a list of numbers, for example. [0, 1, 2, 3, 4] that the for statement will loop around starting at 0 , then it gets to the next number until it reaches the last number 4 . I will explain why it starts from scratch and only reaches four, not five at the end of my answer.

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

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

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

number is a parameter. A parameter is the value that we provide when executing a function from your exit() function by including the value in parentheses () . In this case, we indicated 5, so the list that we created using range will vary from 0 to 4 (0,1,2,3,4 = five numbers in total). If you specify 10 in parentheses, it will create a list starting from 0 to 9. And your code will count from 10 to 1, not from 5 to 1.

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

When Python executes the code inside a for loop , it will first call the print function. Since the for loop starts with 4 , not 5 , we need to do some basic arithmetic and increase the value of each element that we scroll one. We do this by typing + 1 after our variable i .

The reason it starts with 4, not 5, is because the number 0 , not 1 , starts in the programming lists. There is a more detailed technical explanation for the reason that lists start with 0, not 1 (or 4, not 5 in this case, when we canceled the list) here

+7
source

You should call it exit() , not exit .

+4
source

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

 import time, sys messages = [ ("That 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() 
0
source

A good way to do this code:

 def exit(): timer= 5 print "That\ 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() 
-1
source

All Articles