1

I'm trying to write a program that has a function that takes in three arguments and prints one integer.

Specific details:

  • 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 

I can make the program work if I write it like this:

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)

It also works if I write it like this:

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()

But when I try to switch it to this style, it gives no output. I'm confused by the whole sys thing. Please help me understand it.

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()

I also tried like this unsuccesfully:

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()
Barmar
  • 741,623
  • 53
  • 500
  • 612
CTate901
  • 13
  • 4

3 Answers3

1

I got into the same problem, and it is not with sys.argv. The environment for testing the code doesn't show you the errors so it's difficult to tell when your program crashes. There are two possible reasons your code might crash:

  1. Pandas is not installed in your environment
  2. Index out of range error if the third argument is greater than the first

So take out the second line of code(since you're not using pandas) and make sure the third argument is greater than the first before you call the function. The code below worked for me

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)

Hope it helps

0

I have tested your code, but the last two codes yield the expected results as follows:

Your random value is 12

Thus, I think that the causes of your problem may be in a different context.

abysslover
  • 683
  • 5
  • 14
0

Your code worked for me as well- however; if int3 > int1 the program crashes. if you change it to the follow it 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()