【发布时间】:2020-10-30 11:48:55
【问题描述】:
我被要求让程序生成一个由 15 个随机整数组成的数组,然后要求用户输入数组中的数字并让它显示一条消息,说明它在数组中,但是,我得到一个错误。
import numpy as ny
randnums = ny.random.randint(1,101,15)
print(randnums)
target = int(input("Please pick a random number: "))
for counter in range(0,15):
while target != randnums:
print("This number is not in the list")
target = int(input("Please pick a random number: "))
else:
if target == randnums:
print("The number" , target , "has been found in the list.")
输出:
Traceback (most recent call last):
File "python", line 9, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
【问题讨论】:
-
一个数字
target怎么能等于一个包含15个项目的ndarrayrandnums? -
!=不等于。你需要not in -
@rdas 其中一个数字需要在数组中
-
@JohnColeman 是的,如果它不等于目标,那么它需要让用户再次输入数字。
-
@mrKrauklis 但这没有任何意义。单个数字永远不会等于 15 个数字的数组。