PygLatin


#1

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

What criteria do we need to set to make sure the user entered a valid word ? I'm a going based off of if the input is string but i am not sure how to invoke a string type or return the input variable type. Here is my code below:

word = raw_input("Enter a word in english: ")
if type word == type str():
temp = word[0]
word.pop(0)
word = "y" + word + temp + "ay"
print word
else:
print "try again"



#2

@redalati: To make sure the user input's type is a string, simply do:

if type(userInput) == str


If the above condition is True, continue, but if it's False, do not continue.
In this exercise, there's no such need for that but it's always a good practice to think about error handling.

If you want to print the input's type, simply use:

print type(userInput)

Hope I was clear and that it helps you!


#3

Thank you for your response!

I tried this as you suggested:

word = raw_input("Enter a word in english: ")
if type(word) == str:
temp = word[0]
word.pop(0)
word = "y" + word + temp + "ay"
print word
else:
print "try again"

but when I type in "python" as the input for example (without quotes) I get the try again error


#4

@redalati: That's because strings don't have the pop() function, that's for lists.
Strings are immutable so trying to alter a value at a giving position of your string, won't work.
In order to "remove" something from a string, you should use string slicing.


#5

But then why do I receive a "try again" when the first condition type(word) == str: is true? Shouldn't I receive an error if word.pop() runs?

Thank you!


#6

@redalati: I believe it doesn't even get to print anything from it since raw_input will return a string containing the input of the user, not a list.
Here's the proof:


#7

Hmm this is what my output looked like:


#8

@redalati: Check the instructions, please.
Look what they're telling you to print.


#9

Oops sorry it went to the subsequent lesson by default (which builds on the same code) . Here is what I meant to show:

The lesson shows up as complete but it didn't do what it was intended to.