Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | None | 0 0
  1. import random
  2.  
  3. ###def main():
  4.  
  5.  
  6.  
  7. def select_characters()-> str:
  8.     jumbled_word=""
  9.     """Allows the user to enter c or v, and outputs an appropriate random consonant or vowel"""
  10.     count =0 #integer called count, used for counting the number of vowels/consonants
  11.     consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"] #creates a list for all consonants
  12.     vowels = ["a", "e", "i", "o", "u"] # creates a list of the vowels
  13.     vowel_number = 0
  14.     consonant_number=0
  15.     print("\n")
  16.     while count<9:#while loop that loops 8 times
  17.         print("Enter c for consonant or v for vowel")#prompts the user to enter either a c (for consonant) and a v (for vowel)
  18.         user_option = input() #creates a variable called user_option whose value is assigned by the user
  19.         if (user_option=="c" or user_option=="C"): #adds a selection statement for if the input is either a c or C
  20.             print("You have ", 8-count, " letters left")
  21.             random_consonant = random.choice(consonants) #gets a random element from the consonants array
  22.             jumbled_word += random_consonant #appends the random consonant obtained to the word
  23.             count = count+1 #count is incremented by 1
  24.             consonant_number=consonant_number+1 #increments the consonant counter variable by 1
  25.         elif (user_option=="v" or user_option=="V"):  #adds a selection statement for if the input is either a v or V (in case that it is not c )
  26.             print("You have ", 8-count, " letters left") #outputs an appropriate message about the remaining letters
  27.             random_vowel = random.choice(vowels) #gets a random element from the vowels array
  28.             jumbled_word += random_vowel #appends the random vowel obtained to the word
  29.             count = count+1 #count is incremented by 1
  30.             vowel_number = vowel_number+1 #increments the vowel counter variable by 1
  31.         else: #adds a selection statement for if the input is neither c nor v
  32.             print("Error, please enter c or v") #ouputs an appropriate error message, indicating that the input is not valid
  33.     print(jumbled_word) #finally, outputs the word generated
  34.     return (jumbled_word)
  35.  
  36.  
  37. def dictionary_reader(file_name="words.txt" ):
  38.     """Allows the user to enter the filename, to read the information from the specified file"""
  39.     word_list: list =[] #creates an empty list called word_list
  40.     dictionary = open(file_name, "r") #Opens the file in read mode
  41.     word_list= dictionary.read().split("\n") # reads from the file, to a new line each time
  42.     dictionary.close()# closes the file
  43.     print("|Words have been read|") #outputs a message showing  the words
  44.     return word_list # returns the list of words
  45.  
  46.  
  47.    
  48.  
  49.    
  50.        
  51.  
  52.  
  53.  
  54. def word_lookup(  user_characters: str, word_list: list): #takes arguments of string (jumbled and list
  55.     """This function checks what words can be made from the characters generated"""
  56.     matching_words =[] #list to contain words with which the characters match
  57.     for word in word_list: #for loop which goes through each word in the list
  58.         if len(word)<=9: #selection statement for if the word length is longer than 9 letters
  59.             counter1=0 #creates a variable called counter1
  60.             user_letters= user_characters.split() #list which contains the letters that the user generated
  61.             file_words=list(word_list) #list which contains the words from the text file
  62.             processed_characters =[] #stores the characters which have been checked against the word
  63.             for char in file_words:#for the characters in the file_word list
  64.                 if char in user_letters and char not in processed_characters: #checks whether the character is in the letters user generated
  65.                     #and has not been checked against the word
  66.                     if(file_words.count(char)) <=(user_letters.count(char)):#if the character appears in the word
  67.                         #the same number of times, or more in the users string
  68.                         counter1 +=file_words.count(char) # increases count by the number of appearances of the character
  69.                         processed_characters.append(char) #processed character is added to the processed_characters list
  70.             if counter1 == len(word) : #selection statement for if counter1 is equal to the length of word
  71.                 matching_words.append(word) #adds the word to the matching_words list
  72.     print("Enter a guess for the longest word you can make !") #prompts the user to enter their guess for the longest word
  73.     guess=input() #creates an input called guess, which takes an input from the user
  74.     for x in matching_words: #for the number of elements in the matching_words list
  75.         if (matching_words[x]==guess): #if guess is equal to any of the elements in the list
  76.             print("Your guess is a match!") #outputs a message indicating a match
  77.             print("\n")
  78.             print("Your score is ", len(guess)) #outputs a message indicating the score (length of the guess word)
  79.         else:
  80.             print("Your guess does not match") #outputs a message indicating the word does not match
  81.             print("\n")
  82.             print("Your score is 0") #outputs a message indicating that the user's score is 0
  83.         print("These are the words that can be created") #outputs a message indicating the words that can be created
  84.         return matching_words #outputs the list of words that can be created
  85.                    
  86.            
  87.    
  88.                  
  89.  
  90. if __name__ == "__main__":
  91.     """Main programs from which other functions can be called"""
  92.     print("Welcome to the Countdown Simulator \nThe aim of the game is to make the longest possible words using the letters given to you !")
  93.     #title message introducing the user to the program
  94.     print("\n") #new line
  95.     print("Enter PLAY to play the game !") #allows the user to enter their option (kind of useless with only one option )
  96.     user_input=input() #creates a variable called user_input which is what the user inputs
  97.     input_check=False #creates a boolean variable called input_check
  98.     file_check=False #creates a boolean variable called file_check
  99.     while (input_check == False):
  100.         #while input_check is false
  101.         if (user_input=="PLAY"):#if the users input is equal to "PLAY"
  102.             input_check=True #sets the value of the boolean input_check as true
  103.             print("Enter the filename for the dictionary (Null input for default)") #prompts the user to enter the filename for the dictionary
  104.             word_list=[] #creates a list called word_list
  105.             file_name=input() # allows the user to input the name of the file
  106.             while(file_check==False): # adds a while loop, with the condition being the boolean value of the file_check variable
  107.                 if(file_name==""): #selection statement for is the file_name is empty
  108.                     word_list=dictionary_reader() #allocates the words read from the text file  to the list
  109.                     file_check=True # updates the value of file_check to true
  110.                 else:
  111.                     try: #tri catch block
  112.                         word_list=dictionary_reader(file_name) #tries to run the dictionary_reader function with the file_nae argument
  113.                         file_check=True #sets the value of file_check to true
  114.                     except OSError: # in the case of an OSError (internal system error(ie:due to non-existent file))
  115.                          print("Error!, please enter a valid filename") # Promps the user to enter a correct filename
  116.                          file_name=input() #takes another input
  117.             user_word=select_characters() #creates a variable called jumbed_words, which consists of the string from select_characters
  118.             word_lookup(user_word,word_list) #runs the function word_lookup using arguments: jumbled_word and word_list(list of all words)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement