Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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 试图从用户输入获取与已知密钥最接近的值| numpy.core_异常_Python_Python 3.x_Numpy - Fatal编程技术网

Python 试图从用户输入获取与已知密钥最接近的值| numpy.core_异常

Python 试图从用户输入获取与已知密钥最接近的值| numpy.core_异常,python,python-3.x,numpy,Python,Python 3.x,Numpy,我正在尝试编写一个基于文本的体育游戏,游戏的一部分是根据用户输入与已知值的比较来决定谁赢得提示,并根据最接近的用户来决定胜利者 我使用NumPy将一个包含值的列表转换为一个数组,然后找到每个值与K的绝对差,并从中获得最小值 我得到这个错误: Traceback (most recent call last): File "/Users/***/PycharmProjects/MyFirsyPycharm/Sports_Game/basketballgame.py", li

我正在尝试编写一个基于文本的体育游戏,游戏的一部分是根据用户输入与已知值的比较来决定谁赢得提示,并根据最接近的用户来决定胜利者

我使用NumPy将一个包含值的列表转换为一个数组,然后找到每个值与K的绝对差,并从中获得最小值

我得到这个错误:

Traceback (most recent call last):
  File "/Users/***/PycharmProjects/MyFirsyPycharm/Sports_Game/basketballgame.py", line 33, in <module>
    tipoff(player_one_tip, player_two_tip)
  File "/Users/***/PycharmProjects/MyFirsyPycharm/Sports_Game/basketballgame.py", line 11, in tipoff
    tipoff_winner = closest(tipoff_value, 50)
  File "/Users/***/PycharmProjects/MyFirsyPycharm/Sports_Game/basketballgame.py", line 6, in closest
    idx = (np.abs(lst - K)).argmin()
numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U3'), dtype('<U3')) -> dtype('<U3')

不是问答案,但有人能告诉我我错过了什么吗?或者请让我知道,如果我用了完全错误的方法,那是因为您的
输入将数字作为字符串

您应更改为:

player_one_tip = int(input("Player one, select a number between 1 and 100"))

player_two_tip = int(input("Player two, select a number between 1 and 100"))

错误表明您正在尝试减去字符串,而不是数字。你有一个字符串值数组,而不是数字数组。@AndrasDeak我是个大白痴。我将输入更改为int(input())并使其工作。把这个留给像我这样愚蠢的人。非常感谢。不要对自己太苛刻,有些错误在事后看来更为明显:)至少你不太可能再犯这样的错误了。(然而,大多数犯同样错误的人可能找不到你的问题。)谢谢你,先生。我不敢相信我犯了这么愚蠢的错误。更改后,代码现在按预期工作。谢谢你,如果@拉胡尔-维什瓦卡玛解决了你的问题,你应该考虑接受他的答案作为解决方案。
player_one_tip = int(input("Player one, select a number between 1 and 100"))

player_two_tip = int(input("Player two, select a number between 1 and 100"))