简体   繁体   English

无法使用功能

[英]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() . 您可以通过键入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. 我修改了您的倒计时代码,并将其转换为我在exit()内部调用的函数,以演示如何从一段代码中调用一个函数。

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是一个具有一个参数并具有一个目的的函数,用于处理递减计数。

def count_down(number):

The second row contains what we call a for loop . 第二行包含我们所谓的for循环 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. 4开始,然后是3,2,1等。每次循环通过一个数字并且每次只能在循环内部访问时,同一行的变量i都会更改。 The first time print is executed it will be 5, then the next time 4 and so on. 第一次执行打印将是5,然后是下次打印4,依此类推。

 for i in reversed(range(number)):

In this function we also use two additional keywords and one parameter, reversed , range and the parameter number . 在此函数中,我们还使用两个附加关键字和一个参数, reversedrange和参数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 . range用于创建数字列表,例如[0, 1, 2, 3, 4] ,for语句将从0开始循环遍历,然后一直使用下一个数字,直到到达最后一个数字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. reversed用于反转我们使用范围创建的列表。 Since we want to start at 4 , and not 0 . 因为我们要从4开始,而不是0

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

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

number is a parameter. number是一个参数。 A parameter is a value that we provide when we execute the function from your exit() function by including a value inside the parentheses () . 参数是当我们在您的exit()函数中通过在括号()内包含一个值来执行该函数时提供的值。 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). 在这种情况下,我们指定5,因此我们创建的range范围为0-4(0、1、2、3、4 =总共5个数字)。 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. 如果您改为在括号内指定10,则会创建一个从0一直到9的列表。您的代码将从10倒数到1,而不是从5倒数到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 . 当Python开始在for loop上工作时,它将在内部执行代码,从print开始,然后进入sleep ,并继续对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. 当Python在for loop内执行代码时,它将首先调用print函数。 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. 因为for循环将从4开始,而不是5 ,所以我们需要做一些基本的算术运算,并将循环通过的每个项的值加1。 We do this by typing + 1 after our variable i . 为此,我们在变量i后面输入+ 1

The reason why it starts at 4 and not 5 is because in programming lists starts with the number 0 , and not 1 . 它以4而不是5开头的原因是因为在编程列表中以数字0开头而不是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 没有为什么的原因提供更多的技术说明列出从0开始,而不是1(或4,和因为我们颠倒了列表这种情况下,不是5) 这里

您应该将其称为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. 使用保存消息和计时器延迟的tuple ,这样您甚至可以控制每条消息的延迟时间。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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