无法使用某项功能 [英] Can't get a function to work

查看:51
本文介绍了无法使用某项功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个函数,该函数将在游戏结束时以一条倒计时的消息直到结束,并且我不得不在我的文字冒险游戏中重复此代码块很多,因此我决定使用以下功能为了整洁和高效.但是我不知道如何定义和调用这样的函数.这是我要执行的代码:

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

我正在这样调用函数:

elif ready == 'n':
    exit

我在做什么错了?

推荐答案

您可以通过键入 exit()来调用该函数.我修改了您的倒计时代码,并将其转换为在 exit()内部调用的函数,以演示如何从代码段中调用一个函数.

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.

输出:

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

添加了更深入的说明.

第一行 def count_down 是使用一个参数的函数并具有一个目的,以处理倒计时.

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

第二行包含我们所谓的 for循环.此代码的目的是循环遍历对象.从 4 开始,然后从 3,2,1 等开始.每次循环经过a时,同一行的变量 i 都会改变.数字,并且只能在循环内部访问.第一次执行打印将是5,然后是下次打印4,依此类推.

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

在此功能中,我们还使用了两个附加关键字和一个参数,即 reversed range 和参数 number .

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

reversed(range(number))

范围用于创建数字列表例如 [0,1,2,3,4] ,for语句将以 0 开头循环,然后一直使用下一个数字,直到到达最后一个号码 4 .我将解释为什么它从零开始,而到答案末尾只有四个,而不是五个.

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.

已反转用于反转我们使用范围创建的列表.因为我们要从 4 开始,而不是 0 .

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

反转之前 => [0,1,2,3,4]

反转后>后

[4,3,2,1,0]

number 是一个参数.参数是当我们从您的 exit()函数执行函数时提供的值,方法是在圆括号()中包含一个值.在这种情况下,我们指定5,因此我们创建的列表 range 的范围为0-4(0、1、2、3、4 =总共五个数字).如果您改为在括号内指定10,则会创建一个从0一直到9的列表.您的代码将从10倒数到1,而不是从5倒数到1.

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.

Python开始在 for循环上工作时,它将在内部执行代码,从 print 开始,然后从 sleep 开始,并继续执行为 range 创建的列表中的每个数字执行此操作.由于在这种情况下我们指定了五次,因此它将总共执行五次代码.

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.

当Python在 for循环中执行代码时,它将首先调用print函数.因为for循环将从 4 开始,而不是 5 ,所以我们需要做一些基本的算术运算,并将循环通过的每个项的值加1.为此,我们在变量 i 之后键入 +1 .

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.

之所以从4开始而不是从5开始,是因为在编程列表中,它以数字 0 而不是 1 开头.关于列表的原因,还有更多的技术说明.以0开头,而不是1(或4,而不是5,因为我们颠倒了列表)

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

这篇关于无法使用某项功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆