如何使用另一个函数从列表中获取点值 [英] How to use another function to get point values from a list

查看:79
本文介绍了如何使用另一个函数从列表中获取点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不确定该如何解决这个问题,但是据我了解,我想使用另一个函数从52个数字的列表中获取点,然后我想返回一个包含以下内容的列表:列表中不同整数的点之和.

So I am not sure how I am to go about doing this problem, but from my understanding I am suppose to use another function to get points from a list of 52 numbers and then I am suppose to return a list that contains the sum of the points from the different integers from the list.

问题要问:

编写一个函数,该函数将1到1之间的自然数列表作为参数 52(含),并使用练习题3"返回包含以下各项的所有可能总和的列表 与列表中的数字相对应的所有卡的点值.

write a function that takes, as an argument, a list of natural numbers between 1 and 52, inclusive, and uses Practice Problem 3 to return a list containing all of the distinct possible sums of the point values of all of the cards corresponding to the numbers in the list.

因此,从练习3开始,将点值分配给52个整数列表. 代码是:

So to start practice problem 3 assigns point values to a list of 52 integers. It's code:

def getPoints(n):
n = (n-1) % 13 + 1
if n == 1:
    return [1,11]
if 2 <= n <= 10:
    return [n]
if 11 <= n <= 13:
    return [10]

现在,我要使用函数3来创建一个新函数,以获取我从52个数字中选择的数字列表中的点.

Now I am to make a new function utilizing function 3 to obtain points for a list of numbers I choose from the 52 numbers.

这是新功能的代码:

def getPointTotal(aList):
for i in aList:
    points = getPoints(i)
return aList, points

它不完整,因为我被卡住了.现在就坐了.

It isn't complete because I am stuck. Right now as it sits I get.

>>>getPointTotal([10, 1])
>>>[12, 1], [10] # 12 is worth 10 points

所以我注意到它只是从52个数字的列表中取出一个整数,但我不知道如何从列表中获得多个整数.

So I noticed it was only taking one integer from the list of 52 numbers, but I don't know how to make to get more than one of the integers from the list.

我尝试将返回值移动到循环内,但随后它给了我:

I've tried moving the return inside the loop but then it gives me:

>>>getPointTotal([8, 11])
>>>[8, 11], [10] #11 is worth 10 points

如何使被调用函数遍历多个项目?

How do I get the called function to go over more than one item?

推荐答案

您需要附加循环内部的结果,而不要覆盖:

you need to append the results of the inside of your loop, not overwrite:

def getPointTotal(aList):
  points = []
  for i in aList:
      points += getPoints(i)
  return aList, points

这篇关于如何使用另一个函数从列表中获取点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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