【问题标题】:Cartesian Product with user input具有用户输入的笛卡尔积
【发布时间】:2020-09-15 01:12:27
【问题描述】:

我需要编写一个程序,让用户能够输入 2-10 个数字并创建笛卡尔积。这是我到目前为止所拥有的,但第 39 行说“int”对象不可调用。我需要帮助来完成这项工作。

print("list")


firstList=[1,4]
secondList=[2,5,6]
cartesianProduct=0

print=(input("Enter first set of numbers: "))
print=(input("Enter second set of numbers: "))

def CartesianProduct(firstList, secondList):

if len(firstList):
  return None

if len(secondList):
  return None

product=[ ]
for elementA in firstList:
 for elementB in secondList:
   product.append([elementA,elementB])

return product


print(cartesianProduct([firstList], [secondList]))    ##This is line 39

print("End Program")

【问题讨论】:

  • 欢迎来到 StackOverflow。您确定代码缩进正确吗?似乎不是。另外,您能否包含错误的完整回溯?
  • cartesianProduct 不是一个函数,而是一个整数。从上面的程序中,它被定义为等于零。那么你想如何将它用作函数呢?

标签: python-3.x cartesian-product


【解决方案1】:

print(cartesianProduct([firstList], [secondList]))

cartesianProduct 是在input 之前定义的未使用变量。该函数名为CartesianProduct

另外,Python 按照约定使用snake_case,所以函数名应该是cartesian_product

【讨论】:

  • 我改了,它说 Cartesian_product 没有定义
  • 你改变了这两种情况吗?
猜你喜欢
  • 2021-05-06
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 2017-03-31
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多