Python 随机数程序

Python random number program

我正在尝试编写一个程序,该程序具有接受三个参数并打印一个整数的函数。

具体细节:

  • Argument #1 should correspond to the size of a np.randint that has values from 0 to 10.
  • Argument #2 is an integer that you will multiply the randint by.
  • Argument #3 is a value you will index the result of the multiplication by.

Random seed set to 42.

You will print the integer that was indexed as ‘Your random value is x’ where x = the result of the indexing.

python3 reallyrandom.py 59 2 7  

Should generate the following:

Your random value is 12 

如果我这样写,我可以使程序运行:

import numpy as np
import pandas as pd
import random

def reallyrandom(arg1, arg2, arg3):
    
    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))

reallyrandom(59,2,7)

这样写也行:

import numpy as np
import pandas as pd
import random

def reallyrandom():
    arg1=input()
    arg2=input()
    arg3=input()
    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))

reallyrandom()

但是当我尝试将其切换为这种样式时,它没有输出。我对整个系统感到困惑。请帮我理解一下。

import numpy as np
import pandas as pd
import random
import sys

def reallyrandom():
    arg1=sys.argv[1]
    arg2=sys.argv[2]
    arg3=sys.argv[3]
    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))



reallyrandom()

我也这样试过不成功:

import numpy as np
import pandas as pd
import random
import sys

def reallyrandom():

    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))


if __name__ == '__main__':
    arg1=sys.argv[1]
    arg2=sys.argv[2]
    arg3=sys.argv[3]
    reallyrandom()

我已经测试了你的代码,但最后两个代码产生了预期的结果如下:

Your random value is 12

因此,我认为您的问题的原因可能是在不同的上下文中。

你的代码也适用于我——但是;如果 int3 > int1 程序崩溃。如果你把它改成下面的 return nothing if int3 >int1.

import numpy as np
import pandas as pd
import random

def reallyrandom(arg1, arg2, arg3):

    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    if int3 > int1:
        pass

    else:     
        np.random.seed(42)
        x=np.random.randint(0,10, size=int1)
        y=x*int2
        z=y[int3]

        print("Your random value is {}".format(z))

reallyrandom()

   

我遇到了同样的问题,sys.argv 却没有。测试代码的环境不会向您显示错误,因此很难判断您的程序何时崩溃。您的代码可能会崩溃的原因有两个:

  1. Pandas 未安装在您的环境中
  2. 如果第三个参数大于第一个参数,则出现索引超出范围错误

所以取出第二行代码(因为你没有使用 pandas)并确保在调用函数之前第三个参数大于第一个参数。 下面的代码对我有用

import numpy as np
import sys

np.random.seed(42)

arg1=int(sys.argv[1])
arg2=int(sys.argv[2])
arg3=int(sys.argv[3])

def reallyrandom(a, b, c):
    x=np.random.randint(0,10, size=a)
    y=x*b
    z=y[c]

    print(f"Your random value is {z}")


if(arg3 > arg1):
    pass
else:
    reallyrandom(arg1, arg2, arg3)

希望对您有所帮助