Max of Three

Finding the largest of three numbers

In [ ]:
def max_of_3a():
    """Compare each number against every other number."""
    x1, x2, x3 = eval(input("Please enter three values: "))
    
    if x1 >= x2 and x1 >= x3:
        _max = x1
    elif x2 >= x1 and x2 >= x3:
        _max = x2
    else:
        _max = x3
    
    print("The largest value is", _max)
In [ ]:
def max_of_3b():
    """Decision Tree"""
    x1, x2, x3 = eval(input("Please enter three values: "))
    
    if x1 >= x2:
        if x1 >= x3:
            _max = x1
        else:
            _max = x3
    else:
        if x2 >= x3:
            _max = x2
        else:
            _max = x3
    
    print("The largest value is", _max)
In [ ]:
def max_of_3c():
    _max = x1
    if x2 > _max:
        _max = x2
    if x3 > _max:
        _max = x3
        
    print("The largest value is", _max)
In [12]:
from random import sample
def mad_max(qty):
    nums = sample(range(qty*10), k=qty)
    _max = nums[0]
    
    print("Your number list is:", nums)
    for n in nums[1:]:
        if n > _max:
            _max = n
            
    return _max
In [13]:
mad_max(300)
Your number list is: [783, 893, 340, 1216, 109, 1132, 1119, 1676, 2346, 2181, 987, 582, 268, 2746, 372, 220, 595, 2692, 436, 1714, 2859, 2258, 1445, 2882, 2410, 1610, 1048, 2204, 1197, 1572, 2580, 2994, 1359, 462, 919, 430, 1126, 1832, 1680, 1732, 2907, 957, 2890, 2824, 2205, 2809, 781, 2709, 196, 1005, 1336, 952, 1123, 1199, 1550, 1223, 2195, 922, 2085, 34, 1341, 1915, 2879, 56, 2933, 2324, 1547, 1899, 241, 1994, 1159, 837, 772, 1808, 2499, 2330, 1585, 1032, 2236, 1407, 1383, 1464, 1115, 930, 2211, 2391, 225, 1337, 55, 2369, 1065, 1705, 2422, 2771, 1405, 1798, 2758, 381, 1231, 2157, 476, 999, 2781, 2471, 1700, 2912, 989, 1906, 257, 118, 93, 943, 2830, 316, 701, 296, 8, 474, 2022, 967, 631, 2046, 1194, 2277, 1893, 124, 2012, 448, 2668, 935, 872, 199, 1574, 2504, 2332, 2708, 1034, 2923, 641, 68, 415, 916, 1971, 1122, 2043, 1692, 1434, 88, 1312, 1402, 234, 516, 2586, 1514, 1420, 2215, 1631, 2869, 2978, 5, 345, 1920, 2267, 1858, 2501, 131, 2804, 900, 755, 1660, 1575, 160, 204, 1892, 2206, 59, 1503, 531, 226, 1474, 1604, 1370, 2053, 2320, 542, 2446, 1128, 619, 2480, 686, 993, 1794, 747, 416, 609, 2646, 858, 2901, 299, 1006, 594, 871, 215, 904, 1504, 2274, 666, 723, 148, 2333, 2408, 321, 2982, 1185, 983, 1376, 2189, 290, 1293, 418, 1289, 1200, 1371, 422, 1597, 2399, 73, 2833, 2213, 1901, 85, 536, 1039, 733, 1898, 2059, 1736, 2097, 1548, 1354, 223, 2957, 297, 2526, 933, 1075, 2341, 1460, 248, 1765, 563, 1958, 467, 2956, 79, 706, 70, 2194, 839, 1206, 1151, 517, 1020, 1778, 1719, 685, 138, 1741, 1297, 2487, 748, 286, 2780, 2716, 1587, 2731, 2724, 1593, 2967, 2160, 1232, 896, 2962, 39, 2315, 2233, 111, 2652, 598, 2936, 851, 2573, 2159, 2187, 1708, 696, 2004, 890, 105, 2831]
Out[13]:
2994
In [15]:
max(783, 893, 340, 1216, 109, 1132, 1119)
Out[15]:
1216
In [ ]:
file = open("myfile.txt")

for line in file:
    print(line)