Python程序的输出没有意义

时间:2016-01-08 05:26:09

标签: python function python-3.x

我已经在python中编写了一个程序,它显示了最多三个数字。虽然程序很简单,但输出引发了一个问题。这是我编写的代码::

 #Program to find maximum of three numbers using function
def max_of_three(a,b,c):
    if(a>b and a>c):
        print("a is greater")
    elif(b>a and b>c):
        print("b is greater")
    else:
        print("c is greater")
print("Enter three numbers...")
a=int(input())
b=int(input())
c=int(input())
print(max_of_three(a,b,c))

现在,当我运行此程序并在运行时提供输入后获取此输出::

Enter three numbers...
58
45
12
a is greater
None

结果很好..但我不明白为什么“无”这个词会被打印出来?我的意思是什么意思?

2 个答案:

答案 0 :(得分:1)

print(max_of_three(a,b,c))正在尝试打印max_of_three的结果 - 但是没有一个 - 因此None

看起来您希望max_of_three返回字符串而不是直接打印值。这是更好的"因为它分裂了"状态"的显示。从计算。

另一种方法是拨打max_of_three(不使用print),即max_of_three(a,b,c);这是有效的,但现在您的计算始终打印结果(即使您不想要它打印)

答案 1 :(得分:0)

由于您未在函数max_of_three(a,b,c)中返回任何值,因此该函数不返回任何内容,因此输出为None

假设您的评论#Program to find maximum of three numbers using function,您可能意味着返回最大值:

def max_of_three(a,b,c):
    if(a>b and a>c):
        print("a is greater")
        return a
    elif(b>a and b>c):
        print("b is greater")
        return b
    else:
        print("c is greater")
        return c

现在,函数应返回最大值,即58:

Enter three numbers...
58
45
12
a is greater
58
相关问题