大家好,我是 Python 的新手,我正在学习这种编程语言。我正在使用 Python IDE (GUI) 来运行我的所有代码。我已经介绍了定义自定义过程的主题。但是在执行时它没有给出任何输出。
下面是我的代码。我想定义一个将两个数字相加的过程,然后打印我输入的任意两个数字的结果。
def sum(a,b):
print "The Sum Program"
c = sum(10,14)
print "If a is "+a+" and b is "+b++ then sum of the them is "+c
你认为我在这里做错了什么?
请您参考如下方法:
你在这里创建了一个无限循环;在 sum 方法中,您总是调用 sum 方法。
您应该做的是将打印语句移到sum 方法之外。 sum 方法中的内容是返回总和的 return 语句。
因此,您的整个程序应该如下所示(编辑:添加了 str() 调用,感谢@DSM):
# The procedure declaration
def sum(a,b):
return a+b
# Your output code
print "The Sum Program"
a = 10
b = 14
c = sum(a, b)
print "If a is "+str(a)+" and b is "+str(b)+" then sum of the them is "+str(c)
