简体   繁体   中英

Output of a Python program doesn't make sense

I have written one program in python which displays the maximum of three numbers.. Although the program is easy the output is raising a question.. Here is the code that I have written ::

 #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))

Now when I am running this program and getting this output after providing inputs at runtime::

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

The reesult is fine.. But what I don't understand is that why the word "None" is getting printed? I mean what does it mean?

print(max_of_three(a,b,c)) Is trying to print the result of max_of_three - but there isn't one - hence None .

Looks like you intended max_of_three to return a string instead of printing the value directly. This is "better" since it splits the display of the "state" from the calculation.

Alternative would be to just call max_of_three (without print ) ie max_of_three(a,b,c); This works, but now your calculation always prints the results (even if you don't want it to print)

Since you didn't return any values in your function max_of_three(a,b,c) , the function returns nothing, so the output is None .

Assuming by your comment #Program to find maximum of three numbers using function , you may have meant by returning the biggest value:

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

Now, the function should return the greatest value, which is 58:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM