简体   繁体   English

通过Python中的函数传递值

[英]Passing values through functions in Python

I am trying to pass a list of two integers created from one function so that I can manipulate them in succeeding functions. 我试图传递从一个函数创建的两个整数的列表,以便可以在后续函数中操作它们。

    def rollDie(number):
        throw = [] 
        for i in range(number):
            roll = random.randint(1,6)
            throw.append(roll)
        return throw

So I have created another function game() that calls the rollDie() results: 因此,我创建了另一个函数game(),它调用rollDie()结果:

    def game(self,number):
        if self.rollDie[0] != 1 or self.rollDie[1] != 1:
            round_score = self.rollDie[0] + self.rollDie[2]
            return round_score

But when I call the function game() it does not pull the two integers from rollDie(): 但是当我调用函数game()时,它不会从rollDie()中提取两个整数:

    print(game(2))

it returns error: 它返回错误:

    TypeError: game() missing 1 required positional argument: 'number'

I have researched here , here , here among other places inside stackoverflow. 我在这里这里这里以及stackoverflow内部的其他地方进行了研究。 I am hoping someone can help. 我希望有人能提供帮助。 Many thanks for your patience. 非常感谢您的耐心配合。

The way you have defined the function, you have made it a non-static reference, ie it has to be called on an object. 定义函数的方式使之成为非静态引用,即必须在对象上调用它。

You have to call game on whatever object you have defined. 您必须在定义的任何对象上调用游戏。 For example, instantiate your class as game1 and then call game1.game(2). 例如,将您的类实例化为game1,然后调用game1.game(2)。

As @AAA pointed out, the way you are defining game() makes it look like a class function (you can read about classes here ) but it does not look like you have defined a class anywhere. 正如@AAA指出的那样,您定义game()使其看起来像一个类函数(您可以在此处阅读有关类的信息 ),但看起来好像您在任何地方都没有定义类。 If you have defined a class elsewhere, then we need to see that code. 如果您在其他地方定义了一个类,那么我们需要查看该代码。 If you didn't mean to create a class, then you need to take out the self references. 如果您不是要创建一个类,则需要删除self引用。

Also, I am not sure how self.rollDie[0] is supposed to work. 另外,我不确定self.rollDie[0]应该如何工作。 Are you trying to reference a class list called rollDie? 您是否在尝试引用名为rollDie的类列表? If so, I do not see that defined. 如果是这样,我看不到定义。 If you are trying to call your def rollDie(number): function, then you need to do it like so: self.rollDie() . 如果尝试调用def rollDie(number):函数,则需要这样做: self.rollDie() If you want to access the list indices, it would be best to make that equal to something: self.roll_list = self.rollDie(1) which you can then access by self.roll_list[0] 如果要访问列表索引,则最好使其等于: self.roll_list = self.rollDie(1) ,然后可以通过self.roll_list[0]访问

One thing to keep in mind is that lists are mutable objects, so if you are going to use your lists in multiple functions and you do not intend to create a class, thenit may be less confusing to initiate it on its own, outside of a function, as you can access it from any function. 要记住的一件事是,列表是可变的对象,因此,如果您打算在多个函数中使用列表,而又不想创建一个类,则nit可能会更容易混淆,而不是在a之外单独启动它。功能,因为您可以从任何功能访问它。

I did a similar program: 我做了一个类似的程序:

Dice statistics 14.11.2015 骰子统计14.11.2015

from random import sample 来自随机进口样本

result = [0, 0, 0, 0, 0, 0] tosses = 10000 结果= [0,0,0,0,0,0]抛= 10000

for i in range(tosses): dice = sample(range(0, 6), 1) new = str(dice).strip('[]') result[int(new)] += 1 对于i在范围内(掷):骰子=样本(范围(0,6),1)新= str(骰子).strip('[]')结果[int(新)] + = 1

for i in range(0, 6): print(i + 1, ' ====> ', result[i]) 对于范围(0,6)中的i:print(i + 1,'====>',result [i])

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

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