Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
https://code.sololearn.com/cnqkkc7wuCRy/?ref=app
and
https://code.sololearn.com/c802fPXTx057/?ref=app
Do these 2 programs differ ?
If yes, then how so ?
Which one's better ?

What I have tried:

def largest(a,b,c):
	if a>b and a>c:
		print(a)
	if b>a and b>c:
		print(b)
	else:
		print(c)
largest(1,4,7)

```
def max_of_three(a,b,c):
     max_3=0
     if a>b:
         #max_3=a
         if a>c:
             max_3=c
         else:
             max_3=a
     else:
          if b>c:
             max_3=b
          else:
             max_3=c
     return max_3
print(max_of_three(1,4,7))
Posted
Updated 3-Jan-20 4:42am
Comments
Richard MacCutchan 3-Jan-20 10:31am    
The first one makes most sense.
maverickreal 3-Jan-20 10:35am    
Oh I see . Thanks.
Richard MacCutchan 3-Jan-20 10:40am    
If you look closely there is also a bug in the second one, in that it will return the value of c for a call where a is greater than both b and c.
maverickreal 3-Jan-20 10:43am    
Ya that i knew. 👍🏽

1 solution

Both have good and bad features:
The first is more obvious and readable, but the second is more flexible.
A better solution would be to use the logic of the first example, but return a value instead of printing it inside the function.

That way, you get readability and flexibility because the same function can be used from more places:
def largest(a,b,c):
	if a>b and a>c:
		return a
	if b>a and b>c:
		return b
	return c
print(largest(1,4,7))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900