title: Printing the max number in a list without max()
body: <p>I'm having trouble protecting my script from crashing when the user inputs an invalid value to store into a 'number' variable.</p>
  
  <p><strong>Purpose of program:</strong> Allow user to enter integers, stores them into a list, then finds and prints the highest number. Does not allow for strings or floats in input.</p>
  
  <p><strong>Functions:</strong></p>
  
  <ol>
  <li><code>getNum(prompt)</code> -  to test and convert the inputted value to integer and returns y.</li>
  <li><code>swap(A, x, y)</code> - simple swap routine function to sort the data. (Not currently in use)</li>
  <li><code>sortNum(A)</code>  - to sort and use the Swap routine in the list.</li>
  <li><code>maxIt(numbers)</code> - to find the highest number in the list. (IN USE), I don't think I even need to sort the list if I'm using this max function.</li>
  <li><code>main()</code></li>
  </ol>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>def getNum(prompt):
      # Function to call when asking for the numbers later.
      # Try/except to catch crashes if failing to store integer.
      try:
          x = input(prompt)
          y = int(x)
          return y
  
      # Message to print when excepting.
      except:
          print("I was expecting an integer number, please try again... \n")
  
  
  # Function to swap to highest # in list 'numbers'.
  def swap(A, x, y):
      temp1 = A[x]
      A[x] = A[y]
      A[y] = temp1
  
  # Function to perform the looping/swap routine.
  # Program can find max with separate function.
  ## This function is to sort the numbers in the list.
  def sortNum(A):
      for i in range(len(A)):
          for k in range(len(A) - 1):
              first = k
              second = k + 1
  
              if (A[first] &gt; A[second]):
                  # Uses Swap function
                  swap(A, first, second)
  
  # Function to find the highest number in the list.
  def maxIt(numbers):
      maxNum = numbers[0]
      for i in numbers:
          if i &gt; maxNum:
              maxNum = i
      return maxNum
  
  # Start main
  def main():
  
      # Creates the numbers array.
      numbers = []
  
      # Starts the loop to enter numbers into the array.
      done = False
      while not done:
          numInput = getNum("Please enter an integer or &lt; 0 to finish &gt;: ")
  
          # Stores every input from numInput into numbers.
          numbers.append(numInput)
  
          #  Test condition to break out of loop '0'.
          if numInput is 0:
  
              # Prints the resulting max number once finished.
              print("The maximum value is: " + str(maxIt(numbers)))
  
              # Flag to stop the program once finished.
              done = True
  
  main()
  </code></pre>
  
  <p>Current Output when not failing getNum's test (str or float):</p>
  
  <pre class="lang-none prettyprint-override"><code>Please enter an integer or &lt; 0 to finish &gt;: 222
  Please enter an integer or &lt; 0 to finish &gt;: 333
  Please enter an integer or &lt; 0 to finish &gt;: 444
  Please enter an integer or &lt; 0 to finish &gt;: 555
  Please enter an integer or &lt; 0 to finish &gt;: 666
  Please enter an integer or &lt; 0 to finish &gt;: 777
  Please enter an integer or &lt; 0 to finish &gt;: 888
  Please enter an integer or &lt; 0 to finish &gt;: 999
  Please enter an integer or &lt; 0 to finish &gt;: 0
  The maximum value is: 999
  </code></pre>
  
  <p>Errors when entering a str or float into getNum/numInput:</p>
  
  <pre class="lang-none prettyprint-override"><code>Please enter an integer or &lt; 0 to finish &gt;: 222
  Please enter an integer or &lt; 0 to finish &gt;: 333
  Please enter an integer or &lt; 0 to finish &gt;: 444
  Please enter an integer or &lt; 0 to finish &gt;: test
  I was expecting an integer number, please try again...
  
  Please enter an integer or &lt; 0 to finish &gt;: 555
  Please enter an integer or &lt; 0 to finish &gt;: 666
  Please enter an integer or &lt; 0 to finish &gt;: 0
  Traceback (most recent call last):
    File "C:\Users\Bar\Desktop\IS115\Peretz_A9.py", line 64, in &lt;module&gt;
      main()
    File "C:\Users\Bar\Desktop\IS115\Peretz_A9.py", line 59, in main
      print("The maximum value is: " + str(maxIt(numbers)))
    File "C:\Users\Bar\Desktop\IS115\Peretz_A9.py", line 37, in maxIt
      if i &gt; maxNum:
  TypeError: '&gt;' not supported between instances of 'NoneType' and 'int'
  </code></pre>