title: Using a while loop with try and except to get user input
body: <p>My code is creating an infinite loop when I enter a letter at the command prompt.  I think that <strong>len(sys.argv) > 1</strong> is causing the problem, since if I enter a letter at the command prompt this will always be true and the loop will not end.  Not sure how I could work around this though... any advice for this newbie would be helpful.  Thanks!</p>
  
  <pre><code>"""
  Have a hard-coded upper line, n.
  Print "Fizz buzz counting up to n", substituting in the number we'll be counting up to.
  Print out each number from 1 to n, replacing with Fizzes and Buzzes as appropriate.
  Print the digits rather than the text representation of the number (i.e. print 13 rather than thirteen).
  Each number should be printed on a new line.
  """
  
  # entering number at command line: working
  # entering letter at command line: infinite loop
  # entering number at raw_input: works, runs process
  # entering letter at raw_input: working
  
  import sys
  
  n = ''
  
  while type(n)!=int:
      try:
          if len(sys.argv) &gt; 1:
              n = int(sys.argv[1])
          else:
               n = int(raw_input("Please enter a number: "))
      except ValueError:
          print("Please enter a number...")
          continue
  
  print("Fizz buzz counting up to {}".format(n))
  for y in range(1,n+1):
      if y % 5 == 0 and y % 3 == 0:
          print('fizzbuzz')
      elif y % 3 == 0:
          print('fizz')
      elif y % 5 == 0:
          print('buzz')
      else:
          print(y)`enter code here`
  </code></pre>