简体   繁体   English

有人可以为我解释此代码吗?

[英]Could someone explain this code for me?

Can someone explain this piece of code for me, in simple terms. 有人可以简单地为我解释这段代码。

prompts = ("Enter Strength Attribute, between 1 and 50: ", "Enter Skill Attribute, between 1 and 50: ") # I already now this so no need to explain it 

answers = [int(input(p)) for p in prompts]
if not all(0 < a <=50 for a in answers):
    # other code here

Is it a generator? 它是发电机吗?

And how does it work? 以及它如何运作?

Thanks in advance for any answers. 预先感谢您的任何答案。

You have a list comprehension and a generator expression. 您具有列表理解和生成器表达式。

The list comprehension is: 列表理解为:

[int(input(p)) for p in prompts]

and produces a list of integers from a list of prompts, asking the user for a series of numeric values. 并从提示列表中生成一个整数列表,要求用户提供一系列数值。

It could also be expressed as: 它也可以表示为:

answers = []
for p in prompts:
    result = int(input(p))
    answers.append(result)

Next is: 接下来是:

(0 < a <=50 for a in answers)

which is the generator expression. 这是生成器表达式。 It tests if each of the numbers is a value between 0 (exclusive) and 50 (inclusive). 它测试每个数字是否在0(不包括)和50(不包括)之间的值。

The all() function will loop over the generator expression one result at a time, and will return False the moment one of those results is False , or True when it exhausts the generator results and no False values were found. all()函数将循环在发电机表达一个结果的时间,并且将返回False的那一刻的结果之一是False ,或True当它耗尽了发电机结果,没有False发现价值。

You could replace the if all(...) test with: 您可以将if all(...)测试替换为:

result = True
for a in answers:
    if not 0 < a <= 50:
        result = False
        break

if result:

This would achieve the same effect; 这将达到相同的效果; loop over answers one by one, but stop looping early if any of the tests was False (not a number greater than 0 and smaller than or equal to 50). 逐个循环answers ,但如果任何测试为False (不大于0且小于或等于50的数字),则尽早停止循环。

answers = [int(input(p)) for p in prompts]

This is a list comprehension. 这是一个列表理解。 It could be written as a for loop like this: 它可以这样写成for循环:

answers = []
for p in prompts:
    resp = int(input(p))
    answers.append(resp)

if not all(0 < a <=50 for a in answers):

This is a generator, wrapped in all (a built in function that returns whether all elements are true) You could write that as a function: 这是一个发生器,包装在all (一个内置函数,返回是否所有元素都为真),您可以将其编写为一个函数:

def all(answers):
    for a in answer:
        if not 0 < a <= 50:
            return False  # note this short-circuits, stopping the loop
    return True

It is a list comprehension. 这是一个列表理解。

The first line does exactly the same as: 第一行与以下内容完全相同:

answers=[]
for p in prompts:
   a=int(input(p))
   answers.append(a)

The part of the second line behind the if condition does exactly the same as: if条件后面的第二行部分与以下内容完全相同:

for a in answers:
        if a <= 0 or a > 50:
                 return False
return True
for p in prompts

enumerates the prompts 列举提示

int(input(p))

asks the user for an input, using the p as a prompt, then try to coerce the input as an int 要求用户输入,使用p作为提示,然后尝试将输入强制为int

answers = [...]

makes answers a list of all the inputs converted in ints (this is a comprehension list) 使答案列出所有以int转换的输入(这是一个理解列表)

(0 < a <=50 for a in answers)

This is a generator. 这是一个发电机。 It creates an iterable containing the test 0 < a <=50 for each value in the answers list 它为答案列表中的每个值创建一个包含测试0 < a <=50的可迭代

if not all(...)

Tests all elements in the generator. 测试生成器中的所有元素。 if any one is false, do other code 如果任何一个为假,则执行other code

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

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