How to use the .isalpha() function correctly

Chris Warrick kwpolska at gmail.com
Sun Dec 14 12:26:53 EST 2014


On Sun, Dec 14, 2014 at 6:16 PM, Luke Tomaneng <luketomaneng at gmail.com> wrote:
> Here a very small program that I wrote for Codecademy. When I finished, Codecademy acted like it was correct, but testing of this code revealed otherwise.
> --------------------------------------------------
> print 'Welcome to the Pig Latin Translator!'
>
> # Start coding here!
> raw_input("Enter a word:")
> original = str(raw_input)
> if len(original) > 0 and original.isalpha():
>     print original
> else:
>     print "empty"
> --------------------------------------------------
> No matter what I type in, the result is "empty." What do I need to do in order for it to accept words?
> --
> https://mail.python.org/mailman/listinfo/python-list

That’s not where the error is, actually.  You are:

1. taking input with "Enter a word: " and NOT SAVING IT
2. setting original to a string representation of the function
`raw_input`, which is something like

>>> str(raw_input)
'<built-in function raw_input>'

The correct way to do this is:

original = raw_input("Enter a word: ")

as raw_input already outputs a string.

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16



More information about the Python-list mailing list