Pyglatin no input causes error but integers work correctly?


#1

Running this code works in all circumstances except for not entering any input, line 9 throws an error (first = word[0]) as it clearly doesn’t have a string to work with but how do I account for this if the error is occuring when stating variables before my function ?

Pig Latin Translator in Python

pyg = ‘ay’

Get a word from user and store in a variable

original = raw_input(‘Enter a word:’)

Make sure the input is lowercase

word = original.lower()

Store first letter of the input in its own variable

first = word[0]

Create a variable concatenating the input, first letter and 'ay"

new_word = word + first + pyg

Store the rearranged input minus the first letter

new_word = new_word[1:]

Check the user has entered a valid word and print the pyglatin translation

if len(original) > 0 and original.isalpha():
print new_word
else:
print “empty”


#2

From what it sounds like you want, you could find a way to use an if statement that checks to see if the input is a string before running the rest of the code, and if it isn’t you could place a break or stop the code there.

You remember the type() command, right?


#3

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