python - Can't get a function to work -
i try create function end game message counting down until ends, , having repeat block of code lot in text adventure game, decided make function of sake of neatness, , efficiency. cannot figure out how define , call such function. code trying execute:
print "that\'s 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 defining function this:
def exit(): print "that\'s 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 calling function this:
elif ready == 'n': exit
what doing wrong?
you call function typing exit()
. modified countdown code , turned function called inside exit()
demonstrate how call 1 function piece of code.
def exit(): print "that\'s 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): in reversed(range(number)): print i+1 time.sleep(1) exit() # <-- how call exit, missing parentheses @ end.
output:
that's 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
function takes 1 parameter , has 1 purpose, handle count down.
def count_down(number):
the second row contains call for loop. purpose of code loop through objects. starting 4
3,2,1
etc. , variable i
@ same row change each time loop goes through number , accessible inside loop. first time print executed 5, next time 4 , on.
in reversed(range(number)):
in function use 2 additional keywords , 1 parameter, reversed
, range
, parameter number
.
reversed(range(number))
range used create list of numbers e.g. [0, 1, 2, 3, 4]
, statement loop through starting 0
, takes next number way until reaches last number 4
. explain why starts @ 0 , goes four, , not 5 @ end of answer.
reversed used reverse list created range. since want start @ 4
, , not 0
.
before reversed
=> [0,1,2,3,4]
after reversed]
=> [4,3,2,1,0]
number
parameter. parameter value provide when execute function exit()
function including value inside parentheses ()
. in case specified 5, list created range
range 0 - 4 (0,1,2,3,4 = 5 numbers in total). if instead specified 10 within parentheses create list starting 0 way 9. , code count down 10 1, instead of 5 1.
when python has started working on for loop
execute code inside, starting print
, sleep
, , continues for each number in list created range
. since specified 5 in case, execute code total of 5 times.
as python executing code within for loop
first call print function. because loop start @ 4
, not 5
, need basic arithmetics , increase value of each item loop through one. typing + 1
after our variable i
.
the reason why starts @ 4 , not 5 because in programming lists starts number 0
, , not 1
. there more technical explanation available on reason why lists start 0, , not 1 (or 4, , not 5 in case since reversed list) here
Comments
Post a Comment