【问题标题】:Cannot call the variable in formula无法调用公式中的变量
【发布时间】:2017-01-25 07:49:16
【问题描述】:

我正在做这个 python 编程并且遇到了一个问题。 当我把它放在条件语句中时,程序无法调用n_zero的值。

这是程序

import numpy as np

n_zero=int(input('Insert the amount of 0:  '))
n_one =int(input('Insert the amount of 1: '))
n_two =int(input('Insert the amount of 2: '))
n_three = int(input('Insert the amount of 3: '))

data = [0]*n_zero + [1]*n_one + [2]*n_two + [3]*n_three
print len(data)

if len(data)==(2(n_zero)-1):
    np.random.shuffle(data)
    datastring = ''.join(map(str, data))
    print ("Data string is : %s " % datastring )


else:
    print(error)

这是错误

    if len(data)==(2(n_zero)-1):
TypeError: 'int' object is not callable

谢谢

【问题讨论】:

  • 此错误与提供的代码不匹配。请提供实际代码。
  • 对不起。这是错误。 if len(data)==(2(n_zero)-1): TypeError: 'int' object is not callable

标签: python arrays python-2.7 if-statement


【解决方案1】:

在 python 中,您不会强制转换左值变量,因为您没有指定左值类型。

n_zero=int(input('Insert the amount of 0:  '))

关于您的编辑:

您到底想达到什么目标?如果相乘,则使用运算符 *

if len(data)==(2*(n_zero)-1):
    ...

【讨论】:

  • 谢谢!我忘了星号
【解决方案2】:
if len(data) == (2(n_zero) - 1):

应该变成:

if len(data) == (2 * (n_zero) - 1):

请注意第二个示例中的 *。您必须明确提供运算符,如果您不告诉它,Python 不会假设您想将这两个数字相乘。

【讨论】:

  • 谢谢!我忘了星号
猜你喜欢
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
相关资源
最近更新 更多