掷骰子列表

时间:2020-04-03 06:13:15

标签: python list

这就是我希望示例显示的样子。

const int modular = 1e9+7;


int modular_inverse(long long base) {
    long long result = 1;
    int power = modular-2;
    int MOD = modular;
    while(power > 0) {

        if(power & 1) {
            result = (result*base) % MOD;
        }
        base = (base * base) % MOD;
        power >>= 1;
    }
    return result;
}

int main() {
    int a = 27;
    int b = 2048;
    int A = a;
    int B = b;

    for(int i = 0; i < 1e9-1; ++i) {
        A *= a;
        B *= b;
        A = A%modular;
        B = B%modular;
    }
    int B_inv = modular_inverse(B);
    long long res = A*B_inv;
    res = res%mod;
    cout << res << endl;
}

代码

Enter # of dice [3,6]: 4
You have rolled: [9, 3, 8, 8]

您会看到它如何继续添加您在3到6之间输入的每个数字的列表,但我不确定该如何解决。我还觉得我应该放入random.randint来获得随机数,如上面的示例所示。

1 个答案:

答案 0 :(得分:1)

我不清楚rowList的目的在您的代码中是什么,但这和多余的for是造成多余列表的原因。 同样,您永远不会在那里生成随机数。 为此,Python为您提供了random模块。

下面的代码将按预期工作:

import random


def dice():
    n = input( "Enter # of dice [3,6]: " )
    n = int( n )
    diceList = []
    for row in range(0, n):
        diceList.append(random.randint(1, 6))
    print( "\nYou have rolled: ", diceList)


dice()

或者更简洁一些:

import random


def dice():
    n = input( "Enter # of dice [3,6]: " )
    n = int( n )
    diceList = [random.randint(1, 6) for _ in range(0, n)]
    print( "\nYou have rolled: ", diceList)


dice()