创建一个接受三个参数并打印一个整数的函数.

  • 随机种子应设置为42.
  • 第一个参数应该对应于np的大小.randint-0到10之间的值.
  • 第二个参数是一个整数,你可以用它乘以randint.
  • 第三个参数是一个值,您将为乘法结果编制索引

打印索引为"您的随机值为k"的整数

k=索引的结果

The program should not crash if the third value is larger than the first; it should not print anything to the screen.

Code

import sys
import numpy
import random

numpy.random.seed(42)
value1 = int(sys.argv[1])
value2 = int(sys.argv[2])
value3 = int(sys.argv[3])


def randomized(arg1, arg2, arg3):
    x = numpy.random.randint(0, 10, size=arg1)
    y = x * arg2
    return y[arg3]


try:
    random_value = randomized(value1, value2, value3)
    print(f"Your random value is {random_value}")
except IndexError:
    pass

randomized(value1, value2, value3)

Test Case Examples:

python3 reallyrandom.py 1 2 9 
python3 reallyrandom.py 44 3 17
python3 reallyrandom.py 77 -3 55
python3 reallyrandom.py 2 4 10 

Expected Output:

Your random value is 21
Your random value is -9

Error Message

Traceback (most recent call last):
  File "reallyrandom.py", line 23, in <module>
    randomized(value1, value2, value3)
  File "reallyrandom.py", line 14, in randomized
    return y[arg3]
IndexError: index 9 is out of bounds for axis 0 with size 1

推荐答案

The program should not crash if the third value is larger than the first; 100

所以,错误raised是IndexError,对吗?可以防止代码使用try/except块打印任何内容,如下所示:

try:
    # Your code
except IndexError:
    pass # Nothing gets printed

Python相关问答推荐

如何使用scipy从频谱图中回归多个高斯峰?

我从带有langchain的mongoDB中的vector serch获得一个空数组

如何让Flask 中的请求标签发挥作用

管道冻结和管道卸载

在Mac上安装ipython

在Python中动态计算范围

Odoo 16使用NTFS使字段只读

连接一个rabrame和另一个1d rabrame不是问题,但当使用[...]'运算符会产生不同的结果

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

Pandas Loc Select 到NaN和值列表

Python列表不会在条件while循环中正确随机化'

在Python中使用yaml渲染(多行字符串)

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

在Google Drive中获取特定文件夹内的FolderID和文件夹名称

如何按row_id/row_number过滤数据帧

如何在Python中使用Iscolc迭代器实现观察者模式?

Python将一个列值分割成多个列,并保持其余列相同

如何防止html代码出现在quarto gfm报告中的pandas表之上

按条件计算将记录拆分成两条记录

文本溢出了Kivy的视区