0

For a python Bootcamp, I am working on a program which repeatedly asks for the input of a "name" and prints out it out.

When the user enters "bob", the program must print out "oh, not you, bob!", and print out the shortest and longest names previously entered.

If the user enters anything other than a string of characters (for instance, numbers), program must print out an error message and continue on to ask for a name again.

I don't know how to print out an error message when the user enters an int, a float, or something else than a string like 'romeo"

Please see below my program :

 `new_name = ''
  while new_name != 'bob':
  #Ask the user for a name.
   new_name = input("Please tell me someone I should know, or enter 'quit': ")
   print('hey', new_name, 'good to see you')

  if new_name != 'bob':
    names.append(new_name)

  largest = None
  for name in names:
    if largest is None or len(name) > len(largest) :
    largest = name

  smallest = None
  for name in names:
    if smallest is None or len(name) < len(smallest) :
    smallest = name

  print ('oh, not you, bob')
  print ("The smallest name previously entered is :", smallest)
  print("The largest name previously entered is :", largest)

Thank you very much for your help

2 Answers2

1

Try to convert your input to a int if it works its a number.

try:
   user_number = int(input("Enter a name: "))
except ValueError:
   print("That's a good name!")
WasteD
  • 758
  • 4
  • 24
1

You can check if user input contains only letters:

if not new_name.isalpha():
    print 'Only letters are allowed!'

Note: whitespace is also treated as forbidden character.

Eugene Primako
  • 2,767
  • 9
  • 26
  • 35