Small Changes, Big Progress

Posted by Neville on June 1, 2016 under How To, Python, Resources, Training | Comments are off for this article

Going Loopy

The previous post presented code that will produce the same content on each run. Not a particularly useful outcome.

It would be preferable to provide some variation to the input and then to get a more varied output. Before getting into random selection of content to operate on let’s give the user some extra phrases to try to decode, and then ask them to try to work out what each phrase is.

def removeVowels(word):

vowels = ‘aeiouAEIOU’
for j in range(len(word)):

if str(word[j]) in vowels:

wordCopy = word.replace(word[j], ‘*’)
word = wordCopy

return (wordCopy)

words = (‘Lord of the Rings’, ‘Star Wars’, ‘The Jungle Book’, \
‘The Lion King’, ‘Toy Story’)
score = 0
for h in range(len(words)):

hidden = removeVowels(words[h])
print (hidden)
print(‘Can you recognise the movie?’)
respond = input(‘Type it here ‘)
if respond == words[h]:

score += 1

print (‘Score %2d/%d’ % (score, len(words)))

The first few lines (the removeVowels function) is the same as in the previous post, and that code had its last 2 lines removed so that the words variable could be give its value, a tuple containing 5 strings, with each string being a movie name.

To help keep score of the correct guesses a score variable is set to start at 0.

Now to get loopy.

A loop that steps through each string in the words tuple then takes each move title in turn, removes the vowels, prints the phrase minus the vowels, provides instructions to the user and waits for their response. If the response matches the original phrase the score is upgraded and the loop moves on to the next phrase.

When all phrases have been guessed, the code ends.

Moving on.

The next phase of development for this task is to add some randomness since the way that the code is set up right now, the result will always be the same. For now the simplest method for getting different phrases to work on would be to edit the words variable. Although relatively simple, that is inconvenient.

That’s for next time.

 

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Comments are closed.