Exercize to understand from three numbers which is more high

Terry Reedy tjreedy at udel.edu
Fri Jan 25 16:23:29 EST 2019


On 1/25/2019 6:56 AM, ^Bart wrote:
> number1 = int( input("Insert the first number: "))
> 
> number2 = int( input("Insert the second number: "))
> 
> number3 = int( input("Insert the third number: "))
> 
> if number1 > number2 and number1 > number3:
>      print("Max number is: ",number1)
> 
> if number2 > number1 and number2 > number3:
>      print("Max number is: ",number2)
> 
> else:
>          print("Max number is: ",number3)
> 
> Try to insert numbers 3, 2 and 1 and the result will be number 3 and 1, 
> can you help me to fix it?

In my experience based on decades of writing programs...

1. The assignment/exercise/problem should be a write a function with a 
particular signature.  So first decide on the signature.

def max3(n1, n2, n3):
     "Return the max of the three inputs."
     return None  # To be replaced.

2. The next thing to do is to WRITE A TEST.  Any course that does not 
pound this into your head is defective.

In this case, use the numbers 1, 2, 3.  There are 6 permutations of 3 
items, so list them explicitly.  (For more than 3, up to some reasonable 
limit, use itertools.permutations.)

trios = ((1,2,3), ..., (3,2,1))  # Replace ... with the other 4.

for trio in trios:
     m = max3(*trio)
     if m != 3:
         print(f"Error: max3(*{m}) is {m}, not 3.")
print("Test done")

Note: after removing '...,' from trios, I followed my advice and tested 
the above by running it, and caught a couple of typos and improved the 
error message.  Replacing 'None' and '...' is your job.


3. The test will initially fail 6 times.  Good.  Now edit the body of 
max3 until there are none.

4. Worrying about external input comes last.

-- 
Terry Jan Reedy





More information about the Python-list mailing list