类型错误:Int对象不可调用

时间:2015-08-13 06:16:56

标签: python python-3.x

我尝试运行时遇到代码错误。

CODE

print("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division")
print("^ Operation 'ID' please enter the id of your choice")
choice = input()
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
def addition(num1, num2):
    num1
    num2
    ans = num1 + num2
    print('Your answer is %s') %(ans)
def subtraction(num1, num2):
    num1
    num2
    ans = num1 - num2
    print('Your answer is %s') %(ans)
def multiply(num1, num2):
    num1
    num2
    ans = num1 * num2
    print('Your answer is %s') %(ans)
def division(num1, num2):
    num1
    num2
    ans = num1 / num2
    print('Your answer is %s') %(ans)
if choice == "1":
    addition
elif choice == "2":
    subtraction
elif choice == "3":
    multiply
elif choice == "4":
    division
else:
    print("Invalid Input")

一切正常,直到调用python来打印答案。

我知道可能有重复项,但没有提供任何代码。

4 个答案:

答案 0 :(得分:1)

问题是由于这个

if choice == 1():

应该在哪里

if choice == 1:

您必须将num1和num2转换为整数类型

你应该调用该函数,而不是声明它们是

if choice == 1:
    addition(num1, num2)

你必须为其他事情做这件事

完成所有更改后,您的程序将如下所示

print("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division")
print("^ Operation 'ID' please enter the id of your choice")
choice = int(input())
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
def addition(num1, num2):
    ans = num1 + num2
    print('Your answer is %s') %(ans)
def subtraction(num1, num2):
    ans = num1 - num2
    print('Your answer is %s') %(ans)
def multiply(num1, num2):
    ans = num1 * num2
    print('Your answer is %s') %(ans)
def division(num1, num2):
    ans = num1 / num2
    print('Your answer is %s') %(ans)
if choice == 1:
    addition(num1,num2)
elif choice == 2:
    subtraction(num1,num2)
elif choice == 3:
    multiply(num1,num2)
elif choice == 4:
    division(num1,num2)
else:
    print("Invalid Input")

我的更改将是:

choice = int(input("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division\n^ Operation 'ID' please enter the id of your choice\n"))
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
ans=None
if choice == 1:
    ans = num1 + num2
elif choice == 2:
    ans = num1 - num2
elif choice == 3:
    ans = num1 * num2
elif choice == 4:
    ans = float(num1) / num2
if  ans:
    print('Your answer is %s') %(ans)
else:
    print("Invalid Input")

答案 1 :(得分:1)

问题在于 -

if choice == 1():
    addition
elif choice == 2():
    subtraction
elif choice == 3():
    multiply
elif choice == 4():
    division

我不知道你想要1()做什么,好像是一个错字。此外,您应该调用函数addition等,例如 - addition(num1, num2)

choice string不是int。您应该将num1num2转换为int。

代码中存在更多问题 -

  1. 你为什么要在你的函数中做num1num2,它什么都不做,你可以删除每个函数的前两行。

  2. 您的打印功能错误,在Python 3.x中,%(ans)应位于函数内部,而不是在函数外部。

  3. 代码 -

    num1 = int(input("Enter your first number: "))
    num2 = int(input("Enter your second number: "))
    def addition(num1, num2):
        ans = num1 + num2
        print('Your answer is %s' %(ans))
    def subtraction(num1, num2):
        ans = num1 - num2
        print('Your answer is %s' %(ans))
    def multiply(num1, num2):
        ans = num1 * num2
        print('Your answer is %s' %(ans))
    def division(num1, num2):
        ans = num1 / num2
        print('Your answer is %s' %(ans))
    if choice == '1':
        addition(num1, num2)
    elif choice == '2':
        subtraction(num1, num2)
    elif choice == '3':
        multiply(num1, num2)
    elif choice == '4':
        division(num1, num2)
    

答案 2 :(得分:0)

当前代码的错误只是打印语句中的括号。如果不是在打印语句之外使用%ans,代码就像这样

print('Your answer is %s' %ans)

您刚写的相同代码的更简洁版本看起来像这样

print("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division")
print("^ Operation 'ID' please enter the id of your choice")
choice = int(input())
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
def addition(num1, num2):
    return num1+num2
def subtraction(num1, num2):
    return num1-num2
def multiply(num1, num2):
    return num1*num2
def division(num1, num2):
    return num1/num2
if choice == 1:
    ans = addition(num1,num2)
elif choice == 2:
    ans = subtraction(num1,num2)
elif choice == 3:
    ans = multiply(num1,num2)
elif choice == 4:
    ans = division(num1,num2)
else:
    print("Invalid Input")
if choice in range(4):
    print('Your answer is %s' %ans)

答案 3 :(得分:0)

具有讽刺意味的是,最终我写了一个回答我自己的问题的答案,承认来自@ Vignesh Kalai的一些帮助,虽然隐藏代码有点偏。所以在其他任何事情之前,我将解决我的代码的更改。

首先,不是定义我所链接的每个操作,而是定义他们的" ID" s。

其次,我正在使用"" + str(x)打印出答案而不是使用%s的坏主意。

修订后的代码

Query getGuests =  em.createNamedQuery("myq");
@SuppressWarnings("unchecked")
List<Guest> guests = getGuests.getResultList();