#max_in_list.py
import random
import sys
import os

#The task was to find the maximum value in a list.
#The suggested solution is shown below.

#NOTE:  I learned the hard way that I should not use a variable called "max".
# max is a Python function.   I replaced the variable name "max" with "max_val"

list = [22,4,55,6, 19, 8, 54]

num_in_list = len(list)
print("There are ",num_in_list," items in the list.")
      
max_val =0
list_index =0

while (list_index < num_in_list):
    print("list_index = ",list_index," and related value is ",list[list_index])
    
    if (list[list_index] > max_val):
        max_val = list[list_index]
        
    list_index = list_index + 1

print ("Largest value in list is : ", max_val)

#however, the above program will not handle a list of negative numbers correctly

list = [-22, -6,  -18,  -24,  -12 ]
num_in_list = len(list)
print("\nThere are ",num_in_list," items in the list.")
print("The new list is : ", list)
      
max_val =0
list_index =0

while (list_index < num_in_list):
    #print("list_index = ",list_index," and related value is ",list[list_index])
    
    if (list[list_index] > max_val):
        max_val = list[list_index]
        
    list_index = list_index + 1

print ("Largest value in new list is : ", max_val)

#changing the initial value of max to list[0] will permit sorting negative numbers
#Can you see why?

num_in_list = len(list)
print("\nThere are ",num_in_list," items in the list.")
print("The new list is : ", list)
      
max_val =list[0]
list_index =0

while (list_index < num_in_list):
    #print("list_index = ",list_index," and related value is ",list[list_index])
    
    if (list[list_index] > max_val):
        max_val = list[list_index]
        
    list_index = list_index + 1

print ("\nLargest value in list is : ", max_val)

#However,  Python provides a much easier way  to find the maximum value in a list
my_list = [15, 22, 5, 98 ]
print("\nmy_list is = ",my_list)
max_val = max(my_list)
print("\nPython's max function gives ", max_val, "as the largest  value in the list")

