Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何正确返回?_Python_Python 2.7_Numpy - Fatal编程技术网

Python 如何正确返回?

Python 如何正确返回?,python,python-2.7,numpy,Python,Python 2.7,Numpy,我很难正确地返回答案 return roll\u dice(x)语法是否正确,或者是否需要用括号中的其他内容替换x 我是一名初学者,希望获得有关此问题的帮助: 我的代码: import numpy as np def roll_dice(x): totmoney = 0 for a in range(x): throw_one = np.random.randint(6) throw_two = np.random.randint(6)

我很难正确地返回答案

return roll\u dice(x)
语法是否正确,或者是否需要用括号中的其他内容替换
x

我是一名初学者,希望获得有关此问题的帮助:

我的代码:

import numpy as np

def roll_dice(x):
    totmoney = 0

    for a in range(x):
        throw_one = np.random.randint(6)
        throw_two = np.random.randint(6)

        if throw_one % 2 != 0 or throw_two % 2 != 0:
            totmoney += throw_one + throw_two
            print throw_one,"|",throw_two,"|",totmoney
        else:
            totmoney -= throw_one + throw_two
            print throw_one,"|",throw_two,"|",totmoney

        return roll_dice(x)

在不做太多修改的情况下,我认为您想要做的是:

import random

def roll_dice(x):
    totmoney = 0
    result_matrix = []

    for a in range(x):
        throw_one = random.randint(1, 6)
        throw_two = random.randint(1, 6)

        if throw_one % 2 != 0 or throw_two % 2 != 0:
            totmoney += throw_one + throw_two
            print throw_one,"|",throw_two,"|",totmoney
        else:
            totmoney -= throw_one + throw_two
            print throw_one,"|",throw_two,"|",totmoney

        result_matrix.append([throw_one, throw_two, totmoney])

    return result_matrix

example = roll_dice(2)
print example
(我使用了该模块,因为我没有安装numpy)

每次通过循环时,每次创建一行矩阵,最后返回该矩阵

但我要补充一些额外的修改:

import random

def roll_dice(x):
    totmoney = 0
    result_matrix = []

    for a in range(x):
        throws = [random.randint(1, 6), random.randint(1, 6)]

        if throws[0] % 2 != 0 or throws[1] % 2 != 0:
            totmoney += sum(throws)
        else:
            totmoney -= sum(throws)

        print throws[0],"|",throws[1],"|",totmoney

        result_matrix.append([throws[0], throws[1], totmoney])

    return result_matrix

example = roll_dice(2)
print example
以下是我所做的:

  • 我已将您的两次投掷放入名为
    throws
  • 我已经使用该函数添加了这两次抛出
  • 我已将您的
    print
    语句置于您的
    if
    语句之外

我们可以走得更远,但我越来越累了,我不想把你和更高级的东西混淆。

返回骰子(x)
没有break语句,不改变
x的值将导致无限递归循环。您需要在方法中创建一个嵌套列表,该列表表示矩阵,然后以
return my_nested_list
的形式返回该变量?