Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple Python Program #1 of 5 I have 4 other programs posted as well, they are a

ID: 3582231 • Letter: S

Question

Simple Python Program #1 of 5

I have 4 other programs posted as well, they are all simple, but I don't have time to do them. Due in a few hours.

The task is to create a program that will prompt the user for a three-letter word, and do a lookup to see whether it counts as a word in Scrabble.

I’ll lead you through it in several parts, but you’ll have only one program to submit at the end.

Save your program as Problem-1-scrabble-3-lookup.py

(a) I found a reasonable list of three-letter Scrabble words on the web, and copied and pasted them into the text file, Problem-1-three-letter-words.txt.

Take a look at it. The format is 26 lines, one per letter of the alphabet, with all the three-letter words starting with that letter on the line, in alphabetical order, and the lines are separated by blank lines. Your first task is to read the file and create a list containing all the words. You can read the whole file in at once as a string with the command fin.read(), where fin is a filehandle for the file. Then you have to convert the string into a list of words. This will be a list of all the three-letter words, in alphabetical order.

(b) Now write a function that takes any three-letter string as an argument, does a lookup in the list, and returns True if the string is in the list and False otherwise. You are to do the lookup by doing a loop over the list, testing for each item of the list whether the item that was entered is equal to that element of the list.

(c) Next, write code that collects a three-letter string from the user, and prints out a formatted string saying something like, “The string CAT is a valid three-letter word in Scrabble.” or, “The string CAS is not a valid three-letter word in Scrabble.”

Then with a while loop, arrange it so that after the message is printed about whether the string is a word or not, the user is prompted to enter another word. This can go on as long as the user likes. Arrange it so that the user can end the session by entering an empty string, by pressing Enter without typing any letters. When the user enters an empty string, print a message saying something like, “Thanks for using my Scrabble lookup service. Come back again soon.”

Explanation / Answer

def readFile():
   infile = open('Problem-1-three-letter-words.txt', 'r')
   text = infile.read().strip()
   text = text.replace(' ',',')
   wordList = text.split(',')
   return wordList

def inList(wordList, s):
   for word in wordList:
       if (word==s):
           return True
   return False

wordList = readFile()
while True:
   word = raw_input("Enter a 3 letter word to check! ")
   if word=="":
       break
   if (inList(wordList, word)):
       print "The string {} is a valid three-letter word in Scrabble.".format(word)
   else:
       print "The string {} is not a valid three-letter word in Scrabble.".format(word)
print("Thanks for using my Scrabble lookup service. Come back again soon.")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote