Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import msvcrt
- #declaration of consonants and each of their probabilities or being selected
- consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
- consonants_prob = [2,5,11,13,16,18,19,20,25,29,37,41,42,51,60,69,70,71,72,73,74]
- #declaration of the selection available to the user
- selection = []
- def select_characters (): #function to select characters for the user
- while len(selection) != 9: #9 characters will be selected
- user_choice = input("Vowel or Consonant? ").lower()#determine a vowel or consonant
- if user_choice == "v":
- random_num = random.randint(0,67)#select a random number based off of standard distribution of vowels
- if random_num <= 15:
- selection.append("a")
- elif random_num > 15 and random_num <= 36:
- selection.append("e")
- elif random_num > 36 and random_num <= 49:
- selection.append("i")
- elif random_num > 49 and random_num <= 62:
- selection.append("o")
- else:
- selection.append("u") #select according vowel based off of random number selected
- elif user_choice == "c":
- random_num = random.randint(0,74)
- for i in range(0,len(consonants_prob)-1):#iterate through list of consonants probability
- if random_num <= consonants_prob[i]:#if the random number is lower than or equal to
- selection.append(consonants[i])#add that indexed consonant to selection
- break
- i += 1
- else:
- print("Invalid Selection") # ensures user cannot select anything other than a vowel or consonant
- print(selection[-1]) #shows the user the character selected
- time.sleep(0.25)
- print(selection) # displays and returns the selection of characters
- return (selection)
- def leave_game():
- time.sleep(1.5)
- print()
- print("╔╦╗┬ ┬┌─┐┌┐┌┬┌─┌─┐ ╔═╗┌─┐┬─┐ ╔═╗┬ ┌─┐┬ ┬┬┌┐┌┌─┐")
- time.sleep(0.25)
- print(" ║ ├─┤├─┤│││├┴┐└─┐ ╠╣ │ │├┬┘ ╠═╝│ ├─┤└┬┘│││││ ┬")
- time.sleep(0.25)
- print(" ╩ ┴ ┴┴ ┴┘└┘┴ ┴└─┘ ╚ └─┘┴└─ ╩ ┴─┘┴ ┴ ┴ ┴┘└┘└─┘") #prints a thanks for playing text
- def dictionary_reader(filename): #the function to read through the word list
- word_list = []
- f = open(filename+str(".txt")) #opens up the word file
- valid = True
- while valid: #iterates through every line
- item_to_add = f.readline()
- item_to_add = item_to_add.strip('\n')#strips the word to be in correct format
- if item_to_add == "":
- valid = False
- else:
- word_length = item_to_add
- word_length = list(word_length)
- if len(word_length) <= 9: #checks if the word is less than or equal to 9 characters (is possible to be made)
- word_list.append(item_to_add.lower()) # appends to list and returns list
- return word_list
- def get_better_answer(word_list,test_letters): #function to find the best answer the user could have guessed
- valid_list = []
- for x in range(0,len(word_list)-1): #itterates through every word in the word list
- check_word = word_list[x] #assigns each word as the check word
- for i in range(0,len(check_word)):#breaks the word into characters
- if check_word[i] not in test_letters:#check if each letter in in the selectio to the user
- valid = False #if not then break
- break
- elif check_word.count(check_word[i]) > test_letters.count(check_word[i]): #check to see if the number of that char is less than or equal to available
- valid = False
- break #if not then break
- else:
- valid = True
- if valid == True:
- valid_list.append(check_word) #if both conditions met then add to valid list
- sorted_list = sorted(valid_list,key=len) #sort the list in order of length
- first = sorted_list[-1]
- second = sorted_list[-2]
- third = sorted_list[-3] #assign the 3 largest wordsd to variables
- time.sleep(1)
- print("Here are the three best answers you could have got:")
- print(first)
- print(second)
- print(third) #print the three best answers
- leave_game() #call to leave game
- def word_lookup(): #function to look up if the user has entered a valid guess
- test_letters = select_characters()
- word_list = dictionary_reader("words") #declare the selection and word list
- valid = False
- print()
- print("You will have 30 seconds to guess") #tell the user they have 30 seconds
- print()
- print("When you are ready, press Ctrl + C")
- print()
- print("Your Time Starts...")
- time.sleep(2)
- print("Now")
- try:
- for i in range(0,31): #start the loop of 30 seconds
- time.sleep(1) #add each second to the loop
- print(i)
- print("You have run out of time!") #the user has run out of time
- time.sleep(3)
- leave_game() # leave the game
- except KeyboardInterrupt: #if the user has interrupted
- user_guess = input("Please Enter Your Guess: ") #ask for the users guess
- if user_guess == "":
- valid = False #if the answer is blank return an error and repeat input request
- print("Answer cannot be blank")
- if user_guess in word_list: #check if the word is an english word
- user_guess = list(user_guess)
- for i in range(0,len(user_guess)):
- if user_guess[i] not in test_letters: # check if the letters used are in the selection available
- print("Cannot be made using the given characters") #if not tell the user cannot be made
- valid = False
- break
- elif user_guess.count(user_guess[i]) > test_letters.count(user_guess[i]): #check if the chars have been used too many times
- print("You have used a character too many times!") #if yes tell user
- valid = False
- break
- else:
- valid = True
- if valid == True:
- print("Your word is valid!") # if all conditions are met tell user the word is valid
- print("You scored",len(user_guess),"points.") # find the points made from the users guess
- else:
- print("Not a word in the list of words!")
- get_better_answer(word_list,test_letters) #call to find better answers
- def main(): #main function called
- print(" _____ _ _____ ")
- time.sleep(0.25)
- print(" / ____| | | | __ \ ")
- time.sleep(0.25)
- print(" | | ___ _ _ _ __ | |_| | | | _____ ___ __ ")
- time.sleep(0.25)
- print(" | | / _ \| | | | '_ \| __| | | |/ _ \ \ /\ / / '_ \ ")
- time.sleep(0.25)
- print(" | |___| (_) | |_| | | | | |_| |__| | (_) \ V V /| | | |")
- time.sleep(0.25)
- print(" \_____\___/ \__,_|_| |_|\__|_____/ \___/ \_/\_/ |_| |_|") #prints title
- time.sleep(0.25)
- print()
- print("Welcome to Countdown")
- print()
- print("You will be given nine random consonants\nand try to create the largest word possible!")
- print()
- print("Please Select a vowel or consonant (v/c): ") #text to explain what the function is
- word_lookup()
- main() #call main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement