使用raw_input接受函数的值

时间:2013-06-15 22:38:25

标签: python

我希望下面的程序使用raw_input接受a,b,c和d的输入。请修改以下程序以接受a,b,c和d的输入。我尝试这样做

def add(a, b)
    print "Enter the values of a and b"
    a = int(raw_input())
    b = int(raw_input())
    print return a + b

追溯是

File "a.py", line 5
return a + b)
     ^
SyntaxError: invalid syntax

我这样做了,我得到了语法错误。我对所有其他函数做了类似的事情。

def add(a, b):
    print "ADDING %d + %d" %(a, b)
    return a + b

def subtract(a, b):
    print "subtract %d - %d" %(a, b)
    return a - b

def multiply(a, b):
    print "multiply %d * %d" %(a, b)
    return a * b

def divide(a, b):
    print "divide %d + %d" %(a, b)
    return a / b

print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 5)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)

print "Here is a puzzle"

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That is:", what, "How about that?"           

2 个答案:

答案 0 :(得分:2)

问题在于您的add功能。

print return a + b

应该是

return a + b

答案 1 :(得分:0)

from sys import argv
def add(a,b):
    print "Addition %d+%d" %(a,b)
    return a+b

a=int(raw_input())
b=int(raw_input())
#c=add(a,b)
#print "Addition Is %d" %c
print "Addition Is %d" %add(a,b)

尝试通过raw_input()

输入值