Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- ###def main():
- def select_characters()-> str:
- jumbled_word=""
- """Allows the user to enter c or v, and outputs an appropriate random consonant or vowel"""
- count =0 #integer called count, used for counting the number of vowels/consonants
- 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
- vowels = ["a", "e", "i", "o", "u"] # creates a list of the vowels
- vowel_number = 0
- consonant_number=0
- print("\n")
- while count<9:#while loop that loops 8 times
- print("Enter c for consonant or v for vowel")#prompts the user to enter either a c (for consonant) and a v (for vowel)
- user_option = input() #creates a variable called user_option whose value is assigned by the user
- if (user_option=="c" or user_option=="C"): #adds a selection statement for if the input is either a c or C
- print("You have ", 8-count, " letters left")
- random_consonant = random.choice(consonants) #gets a random element from the consonants array
- jumbled_word += random_consonant #appends the random consonant obtained to the word
- count = count+1 #count is incremented by 1
- consonant_number=consonant_number+1 #increments the consonant counter variable by 1
- 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 )
- print("You have ", 8-count, " letters left") #outputs an appropriate message about the remaining letters
- random_vowel = random.choice(vowels) #gets a random element from the vowels array
- jumbled_word += random_vowel #appends the random vowel obtained to the word
- count = count+1 #count is incremented by 1
- vowel_number = vowel_number+1 #increments the vowel counter variable by 1
- else: #adds a selection statement for if the input is neither c nor v
- print("Error, please enter c or v") #ouputs an appropriate error message, indicating that the input is not valid
- print(jumbled_word) #finally, outputs the word generated
- return (jumbled_word)
- def dictionary_reader(file_name="words.txt" ):
- """Allows the user to enter the filename, to read the information from the specified file"""
- word_list: list =[] #creates an empty list called word_list
- dictionary = open(file_name, "r") #Opens the file in read mode
- word_list= dictionary.read().split("\n") # reads from the file, to a new line each time
- dictionary.close()# closes the file
- print("|Words have been read|") #outputs a message showing the words
- return word_list # returns the list of words
- def word_lookup( user_characters: str, word_list: list): #takes arguments of string (jumbled and list
- """This function checks what words can be made from the characters generated"""
- matching_words =[] #list to contain words with which the characters match
- for word in word_list: #for loop which goes through each word in the list
- if len(word)<=9: #selection statement for if the word length is longer than 9 letters
- counter1=0 #creates a variable called counter1
- user_letters= user_characters.split() #list which contains the letters that the user generated
- file_words=list(word_list) #list which contains the words from the text file
- processed_characters =[] #stores the characters which have been checked against the word
- for char in file_words:#for the characters in the file_word list
- if char in user_letters and char not in processed_characters: #checks whether the character is in the letters user generated
- #and has not been checked against the word
- if(file_words.count(char)) <=(user_letters.count(char)):#if the character appears in the word
- #the same number of times, or more in the users string
- counter1 +=file_words.count(char) # increases count by the number of appearances of the character
- processed_characters.append(char) #processed character is added to the processed_characters list
- if counter1 == len(word) : #selection statement for if counter1 is equal to the length of word
- matching_words.append(word) #adds the word to the matching_words list
- print("Enter a guess for the longest word you can make !") #prompts the user to enter their guess for the longest word
- guess=input() #creates an input called guess, which takes an input from the user
- for x in matching_words: #for the number of elements in the matching_words list
- if (matching_words[x]==guess): #if guess is equal to any of the elements in the list
- print("Your guess is a match!") #outputs a message indicating a match
- print("\n")
- print("Your score is ", len(guess)) #outputs a message indicating the score (length of the guess word)
- else:
- print("Your guess does not match") #outputs a message indicating the word does not match
- print("\n")
- print("Your score is 0") #outputs a message indicating that the user's score is 0
- print("These are the words that can be created") #outputs a message indicating the words that can be created
- return matching_words #outputs the list of words that can be created
- if __name__ == "__main__":
- """Main programs from which other functions can be called"""
- print("Welcome to the Countdown Simulator \nThe aim of the game is to make the longest possible words using the letters given to you !")
- #title message introducing the user to the program
- print("\n") #new line
- print("Enter PLAY to play the game !") #allows the user to enter their option (kind of useless with only one option )
- user_input=input() #creates a variable called user_input which is what the user inputs
- input_check=False #creates a boolean variable called input_check
- file_check=False #creates a boolean variable called file_check
- while (input_check == False):
- #while input_check is false
- if (user_input=="PLAY"):#if the users input is equal to "PLAY"
- input_check=True #sets the value of the boolean input_check as true
- print("Enter the filename for the dictionary (Null input for default)") #prompts the user to enter the filename for the dictionary
- word_list=[] #creates a list called word_list
- file_name=input() # allows the user to input the name of the file
- while(file_check==False): # adds a while loop, with the condition being the boolean value of the file_check variable
- if(file_name==""): #selection statement for is the file_name is empty
- word_list=dictionary_reader() #allocates the words read from the text file to the list
- file_check=True # updates the value of file_check to true
- else:
- try: #tri catch block
- word_list=dictionary_reader(file_name) #tries to run the dictionary_reader function with the file_nae argument
- file_check=True #sets the value of file_check to true
- except OSError: # in the case of an OSError (internal system error(ie:due to non-existent file))
- print("Error!, please enter a valid filename") # Promps the user to enter a correct filename
- file_name=input() #takes another input
- user_word=select_characters() #creates a variable called jumbed_words, which consists of the string from select_characters
- 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