1

im trying to check that the input the user enters is either a single letter or word and not an integer or nothing, if they enter something which isnt valid then they should be kept in the loop until they enter a single letter. this is my code so far but it doesnt seem to be working in the desired way:

animalcount = 0
animal = 0
data = False
while data == False:
    try:
        letter = str(input("what letter would you like to search for? "))
        data = True
    except:
        print ("please enter a letter, try again.")
    if letter == "":
        print ("please enter an item,try again!")
        data = False
for animal in animallist:
    if letter in animal:
        print(animal)
        animalcount = animalcount + 1
if animalcount == 0:
    print ("That letter cannot be found")
Sam
  • 7,252
  • 16
  • 46
  • 65
LadyGeek
  • 11
  • 1
  • 3

2 Answers2

1

Making a string from input will not achieve much since anything you enter with the keyboard will work perfectly fine as a string.

You could use a regexp to distinguish between letters and numbers.

Jonatan
  • 1,096
  • 1
  • 18
  • 28
0

You could just use a simple loop at the top.

import string
letter = None
while not letter or letter not in string.letters:
    letter = str(raw_input("What letter would you like to search for? "))

Then the rest should work.

sotapme
  • 4,695
  • 2
  • 19
  • 20