Pop Quiz question


#1



So when I press enter on the first input it goes to the else block but if I push enter again without an input it just prints none. How do I have the else block repeat itself if there is still an empty input?

Hope i got this across correctly.


print 'Welcome to the Pig Latin Translator!'

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

if len(original) > 0 and original.isalpha():
    print original 
    
else:
    print raw_input("Please try another word:")


#2

It also seems like anything I write in the else block will be counted towards a "completion"

Welcome to the Pig Latin Translator!
Enter a word:
Please try another word: 3
3
None

^^^
No errors were found apparently.


#3

else will not cause the code to run again, else will just run once if the if condition evaluates to true, to repeat we need a loop:

original = raw_input("Enter a word:")
while len(original) == 0 or not original.isalpha():
    print raw_input("Please try another word:")

Are loops covered at this point in the course?

Not sure this code is wise to use in the exercises, just do as the exercises tell you, that will get you in the least trouble


#4

My mistake, I thought that was an error but I guess that will be covered later on. Thank you for your help though, will keep that in mind for the near future.


#5

Loop will be taught later, you could also use a recursive function call given functions are taught:

def valid_input():
    original = raw_input("Enter a word:")
    if len(original) > 0 and original.isalpha():
       return original
    else:
       return valid_input()
original = valid_input()
print original

not as clean as the loop, but covered at this point in the course :slight_smile:


#6

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