Pyglatin 5/11


#1

Hello Forum,

My code is below. The example notes that any non-letter characters will return a False statement?

When I ran the code below and typed “B1” as an example within the editor to test the original.isalpha() command, expecting a reply of “False”. the reply that came up within the editor said “Empty”. Should the reply not have said False?

Please can somebody explain why false did not appear as per Codecademy examples and give examples of code that would reply that “False” response.
Thank you in advance
Brendan

Blockquote

original = raw_input(“What is yout name”)
if len(original) > 0 and original.isalpha():
print original
else:
print “empty”

Blockquote


#2

Hey there.

Your code ran correctly, giving you the response “Empty” when you entered “B1” at the prompt.

The reason you didn’t get “False” is because you’re not just printing the output of original.isalpha(). Instead, you’re using that as one of the conditions in your if-else block.

isalpha will only return True if the string it is called against is made entirely of letters.

Your code is asking for input, and assigning this to original. If the user provides input, making len(original) a non-zero value, and that input is entirely alphabetical, your if branch is True and you call print original.

Anything else causes your else block to be used.

Does that make sense?


#3

original.isalpha() does exactly what you expected it to do. It returned false when you gave “B1” as input.

Should the reply not have said False?

It doesn’t say “False” because you haven’t told it to. You told the interpreter to print “empty”. As you can see here:

else:
    print("empty")

You may still be confused, so I’m going to go through your script step by step.

original = raw_input("What is yout name")

This tells the interpreter to assign original whatever value you write into stdin (what you type on your keyboard). You probably already know how to use it.

if len(original) > 0 and original.isalpha():

This tells the interpreter to execute whatever instructions is written below it(the indentation marks what code should be run) as long as the condition given is true (you probably already knew that too).
Now let’s try to deduce if the condition will be true or false.

len(original) > 0

This checks if you’ve entered anything at all. Let’s say you typed “B1”. This is true so we already substitute that small condition through a boolean in the big condition:

True and original.isalpha()

Now let’s look at original.isalpha():

original.isalpha()

This returns true if there are only letters in original. Now, this is not the case with “B1”, so we now know it’s false. Let’s look at our condition again:

True and False

True and False is false. Because, for the output of an and gate to be true, all the inputs also need to be true. As this is not the case, the condition is false. Let’s look at our if statement now:

if False:

This won’t execute the code written below it so we are going to look at the next command:

else:

This tells the interpreter that if the last condition was false to execute the code below it. As the last condition was false, it is what we are going to do.

print "empty"

This tells to write “empty” to stdout (in this case our screen). So, now we read “empty” which is exactly what you said happened.

So this code simply says if input was given and the input is only constituted of letters, output the input. If that was not the case, it should output “empty”.


#4

Thank you both thepitycoder and festerdam. Without the kind help of people in this forum, i would completely struggle. Both comments are understood above, so thank you.

One thing to add to my original query, i discovered this by accident within the previous code noted. Starting with a space before ie. typing a name results in a false result being produced too (assume this is because original.isalpha is to establish if there are any non-letter characters within the code). So my question is as followsL How does one incorporate a space between the coded question ie.What is your name: and the resulting console reply by the person typing in an answer. Thank you


#5

You could write a function that iterates over a string and checks if any character in it isn’t alpha and isn’t white space(you can use string.whitespace from the string module) and returns False if that’s the case and True otherwise. You could also find another way to do it.