如何在python中将我的函数数值输出为列表

如何在python中将我的函数数值输出为列表,python,function,Python,Function,以下是我的功能: def seq_sum(n): """ input: n, generate a sequence of n random coin flips output: return the number of heads Hint: For simplicity, use 1,0 to represent head,tails """ flip = 0 heads = 0 while flip <= n:

以下是我的功能:

def seq_sum(n):
    """ input: n, generate a sequence of n random coin flips
        output: return the number of heads 
        Hint: For simplicity, use 1,0 to represent head,tails
    """
    flip = 0
    heads = 0
    while flip <= n:
        coin = random.randint(0,2)
        flip += 1
        if coin == 1:
            heads += 1

    print(heads)
等等。但我想要的是头的数量,再加上输出的列表:

55

[1, 0, 2, 1, .....]
当我尝试打印(列表(标题))时,我收到以下错误消息:

TypeError: 'int' object is not iterable

TypeError:“int”对象不可编辑

希望此代码能满足您的需要

def seq_sum(n):
    flips = [random.randint(0, 1) for _ in range(n)]
    return sum(flips), flips
用法

从代码中的注释可以看出,函数应该只返回头的数量

def seq_sum(n):
    return sum(random.randint(0, 1) for _ in range(n))

在您的功能中,您只是跟踪总人数,而不是他们的历史。您需要创建一个iterable集合来保存临时值,例如列表或Numpy数组

import numpy as np

def seq_sum(n):
    flips = np.random.randint(low=0, high=2, size=n)
    return sum(flips), flips.tolist()

# Example usage.
total_heads, flips = seq_sum(n=10)

请注意,对于numpy的
randint
函数,起点和终点分别是包含的和独占的。

我不知道我是否理解正确,但下面是我非常简单的解决方案

import random

def seq_sum(n):
""" input: n, generate a sequence of n random coin flips
    output: return the number of heads 
    Hint: For simplicity, use 1,0 to represent head,tails
"""
  flip = 0
  heads = 0
  outcomes=[]
  while flip < n:
    coin = random.randint(0,2)
    outcomes.append(coin)
    flip += 1
    if coin == 1:
        heads += 1

  print(heads)
  print(outcomes)


--------
当我尝试打印(列表(标题))时,我收到以下错误消息:

TypeError: 'int' object is not iterable
让我们从这里开始<代码>头从开始到结束始终为整数。因此,当您将
list()
放在
heads
周围时,Python会抱怨您输入了
list
一个整数,这是不可编辑的

外卖1:某些对象只能接受某些类型的参数

第二站:我想要一个列表来存储正面和反面。我该怎么办?一种方法是创建一个列表来存储它

my_list = [] # or list()
要添加到列表中,可以使用
append
方法
append
方法在列表末尾添加一个元素

my_list.append(1)
# now my_list is [1]
my_list.append(0)
# now my_list looks like [1, 0]
第三个目标:我想随机生成0和1来表示尾和头。你真的在做你想做的吗?对你调用的函数要小心,尤其是那些你不熟悉的函数。了解函数的作用

random.randint(a,b) 返回一个随机整数N,以便
导入随机数
#就地编辑
定义序号和(n):
“”“输入:n,生成n个随机抛硬币序列
输出:返回头数
提示:为简单起见,请使用1,0表示头、尾
"""
翻转=0
人头=0
seq=列表()

虽然flip不是单个函数调用的输出,但我猜是多个函数调用的输出?函数中没有任何地方可以返回任何内容,只有打印。如果随机选择0和1,为什么不让它们使用sum表示正反面?创建结果列表,并在每次循环旋转时追加硬币。然后从函数返回结果。随机范围包含
2
的含义是什么?当头部为
1
且尾部为
0
。。。它是一个三面硬币吗?
random.randint
返回一个范围为[a,b]的随机整数,包括两个端点。因此,您需要使用
random.randint(0,1)
。我想到了这一点,但由于随机范围包括(0,1或2),那么结果将是错误的,因为您假设只有头(这就是为什么您对列表求和)。我认为问题本身有点错误。我认为范围包括(0或1)以确保您可以查看代码中的注释这是真的,它在注释中,但范围是
random.randint(0,2)
问题中的两个输出都包含2,这很奇怪。作业特别要求我在函数中使用random.rand,并且我不能导入任何库。对函数的调用已由教师编写:(
my_list = [] # or list()
my_list.append(1)
# now my_list is [1]
my_list.append(0)
# now my_list looks like [1, 0]
def bar():
    return 1, 2

c = bar()  # c is a tuple that holds two values, 0 and 1 now!
# c[0] == 1 # access the first element with index 0
# c[1] == 2 
import random

# edit in place
def seq_sum(n):
    """ input: n, generate a sequence of n random coin flips
        output: return the number of heads 
        Hint: For simplicity, use 1,0 to represent head,tails
    """
    flip = 0
    heads = 0
    seq = list()
    while flip <= n:
        coin = random.randint(0,2)
        seq.append(coin)
        flip += 1
        if coin == 1:
            heads += 1

    print(heads,seq)


#solution 2
def seq_sum(n):
    flip = 0
    seq = list() #create a list to store every random value
    while flip < n: # if want to generate n values, the while loop only need compare 0,1,2,...n-1 times, so you need <, not <=.
        coin = random.randint(0,1)  # coin has only two sides
        seq.append(coin)
        flip += 1
    return seq
    # print(heads) # it is not good idea to print data in function.
random_list = seq_sum(55)
head_num = sum(random_list)