Here’s what I have so far:
import string
So I have the user write a 5 worded sentence asking for only 5 words:
def main(sentence = raw_input("Enter a 5 worded sentence: ")):
if len(words)<5:
words = string.split(sentence)
wordCount = len(words)
print "The total word count is:", wordCount
If the user inputs more than 5 words:
elif len(words)>5:
print 'Try again. Word exceeded 5 word limit'
Less than 5 words:
else:
print 'Try again. Too little words!'
It keeps stating that:
UnboundLocalError: local variable 'words' referenced before assignment
Your problem is that you are calling
len(words)before the variablewordsexists. This is in the second line of your second code block.Note that in python, default arguments are bound at time of function definition rather than at function call time. This means your
raw_input()will fire when main is defined rather then when main is called, which is almost certainly not what you want.