PygLatin specific response for inputs with numbers


#1



https://www.codecademy.com/en/courses/python-beginner-2W5v7/0/6?curriculum_id=4f89dab3d788890003000096

Hello, I'm at PygLatin section of the Python course, at the popquiz section.
I did what the instructions told me to and there is no problem with that.But to get a better understanding of the if, elif etc statements, i tried to add a piece of code which was supposed to print "Your code contains numbers" instead of just printing "empty".

So, I added this piece of code to the original code expecting the terminal to display only the "Your code contains numbers" message but when I type a word combined with numbers the output is:

Your word contains numbers
empty
None

Below is the full code. Any help would be appreaciated.

' ' '

if not original.isalpha():

    print "Your word contains numbers"

' ' '


print 'Welcome to the Pig Latin Translator!'

# Start coding here!
original = raw_input("Enter a word: ")

if not original.isalpha():
    
        print "Your word contains numbers"
        
if len(original) > 0 and original.isalpha():
    print original
    
else: 
    len(original) <= 0
    print ("empty")
if original.isalpha():
    if False:
        print "Your word contains numbers"


#2

if and else are to be used ONCE (generally -- does not really apply when doing nests where you have multiple ifs and elses nested in if statements). if for the very first, else for the very last. ALSO, else should never have a condition like this:

else i == "foo": #This will cause SYNTAX ERROR in Python

So, what if you have multiple if statements? You can use elifs for the ones in between. Think of this as the items between the breads of a hamburger (where if and else are the breads).
General structure:

if i == "foo":
    print "foo"
elif i =="foo foo":
    print "foo foo"
elif i == "foo foo foo":
    print "foo foo foo"
else:
    print "hello"

EDIT:
Nested if statements are like a hamburger in a hamburger.


#3

Thanks a lot, really appreciated the fast response as well as the simple explanation :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.