Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Validate the user's input

Hello - i am trying to validate if a user's input is alphabet or not. Below is the code i wrote. Its giving this error - TypeError: object of type 'NoneType' has no len() I understand why its giving that error, because initially the variable 'guess' doesn't contain anything to validate, but i want it to validate after the user enters their input. How do i achieve this? what am i doing wrong?

Thank you

validate.py
import random

words = ['apple',
         'orange',
         'lemon',
         'mango',
         'melon']
secret_guess = random.choice(words)
bad_guesses = []
good_guesses = []
secret_guess1 = list(secret_guess)
guess = print("Guess a letter")
while True:
  if len(guess) != 1:
        print("Cannot enter more than one lttter")
        continue
  elif guess.isalpha() == False:
      print("Cannot enter anything else than alphabet")
      continue

3 Answers

Are you wondering about how to get your program running or just the while loop part? Your program is currently crashing at line 14 and you are getting a "TypeError: object of type 'NoneType' has no len()" error. You have 'guess = print("Guess a letter")' instead of 'guess = input("Guess a letter")'. This means the computer does not ask you for your letter and you are actually creating a '<class 'NoneType'>' object for guess. This basically just means that guess does not have any data in it. Don't worry about how or why this happens for now. When you go to get the len() of guess python does not know what to do and crashes.

For the while loop part I would move 'guess = input("Guess a letter")' to the first thing in the while loop. This would let the user keep entering their guess each time through the while loop. You could then get rid of the two continues and add a break to the end if the user entered a correct guess. These two things (The guess variable in the while loop and the break) would allow the user to keep entering their guess until it is an actual letter. Then the program would continue.

If you have any other questions or need more information or explanations about anything feel free to let me know.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Do you mean to use input("Guess a letter") instead of print("Guess a letter")?

The print function returns None as the result. Hence, the error "NoneType has no len()"

>>> foo = print("hello")
hello
>>> foo
>>> foo is None
True

Got it. Thanks a lot!

Your welcome.