Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.63 KB | None | 0 0
  1. import random
  2. import time
  3. import msvcrt
  4.  
  5.  
  6. #declaration of consonants and each of their probabilities or being selected
  7. consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
  8. consonants_prob = [2,5,11,13,16,18,19,20,25,29,37,41,42,51,60,69,70,71,72,73,74]
  9.  
  10. #declaration of the selection available to the user
  11. selection = []
  12. def select_characters (): #function to select characters for the user
  13.     while len(selection) != 9: #9 characters will be selected
  14.         user_choice = input("Vowel or Consonant? ").lower()#determine a vowel or consonant
  15.         if user_choice == "v":
  16.             random_num = random.randint(0,67)#select a random number based off of standard distribution of vowels
  17.             if random_num <= 15:
  18.                 selection.append("a")
  19.             elif random_num > 15 and random_num <= 36:
  20.                 selection.append("e")
  21.             elif random_num > 36 and random_num <= 49:
  22.                 selection.append("i")
  23.             elif random_num > 49 and random_num <= 62:
  24.                 selection.append("o")
  25.             else:
  26.                 selection.append("u") #select according vowel based off of random number selected
  27.         elif user_choice == "c":
  28.             random_num = random.randint(0,74)
  29.             for i in range(0,len(consonants_prob)-1):#iterate through list of consonants probability
  30.                 if random_num <= consonants_prob[i]:#if the random number is lower than or equal to
  31.                     selection.append(consonants[i])#add that indexed consonant to selection
  32.                     break
  33.                 i += 1
  34.         else:
  35.             print("Invalid Selection") # ensures user cannot select anything other than a vowel or consonant
  36.         print(selection[-1]) #shows the user the character selected
  37.         time.sleep(0.25)
  38.     print(selection) # displays and returns the selection of characters
  39.     return (selection)
  40.  
  41.  
  42.  
  43.  
  44.  
  45. def leave_game():
  46.     time.sleep(1.5)
  47.     print()
  48.     print("╔╦╗┬ ┬┌─┐┌┐┌┬┌─┌─┐  ╔═╗┌─┐┬─┐  ╔═╗┬  ┌─┐┬ ┬┬┌┐┌┌─┐")
  49.     time.sleep(0.25)
  50.     print(" ║ ├─┤├─┤│││├┴┐└─┐  ╠╣ │ │├┬┘  ╠═╝│  ├─┤└┬┘│││││ ┬")
  51.     time.sleep(0.25)
  52.     print(" ╩ ┴ ┴┴ ┴┘└┘┴ ┴└─┘  ╚  └─┘┴└─  ╩  ┴─┘┴ ┴ ┴ ┴┘└┘└─┘") #prints a thanks for playing text
  53.    
  54.  
  55. def dictionary_reader(filename): #the function to read through the word list
  56.     word_list = []
  57.     f = open(filename+str(".txt")) #opens up the word file
  58.     valid = True
  59.     while valid: #iterates through every line
  60.         item_to_add = f.readline()
  61.         item_to_add = item_to_add.strip('\n')#strips the word to be in correct format
  62.         if item_to_add == "":
  63.             valid = False
  64.         else:
  65.             word_length = item_to_add
  66.             word_length = list(word_length)
  67.             if len(word_length) <= 9: #checks if the word is less than or equal to 9 characters (is possible to be made)
  68.                 word_list.append(item_to_add.lower()) # appends to list and returns list
  69.     return word_list
  70.  
  71.  
  72. def get_better_answer(word_list,test_letters): #function to find the best answer the user could have guessed
  73.     valid_list = []
  74.     for x in range(0,len(word_list)-1): #itterates through every word in the word list
  75.         check_word = word_list[x] #assigns each word as the check word
  76.         for i in range(0,len(check_word)):#breaks the word into characters
  77.             if check_word[i] not in test_letters:#check if each letter in in the selectio to the user
  78.                 valid = False #if not then break
  79.                 break
  80.             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
  81.                 valid = False
  82.                 break #if not then break
  83.             else:
  84.                 valid = True
  85.         if valid == True:
  86.             valid_list.append(check_word) #if both conditions met then add to valid list
  87.        
  88.            
  89.     sorted_list = sorted(valid_list,key=len) #sort the list in order of length
  90.     first = sorted_list[-1]
  91.     second = sorted_list[-2]
  92.     third = sorted_list[-3] #assign the 3 largest wordsd to variables
  93.     time.sleep(1)
  94.     print("Here are the three best answers you could have got:")
  95.     print(first)
  96.     print(second)
  97.     print(third) #print the three best answers
  98.     leave_game() #call to leave game
  99.  
  100.  
  101.  
  102.  
  103.  
  104. def word_lookup(): #function to look up if the user has entered a valid guess
  105.     test_letters = select_characters()
  106.     word_list = dictionary_reader("words") #declare the selection and word list
  107.     valid = False
  108.     print()
  109.     print("You will have 30 seconds to guess") #tell the user they have 30 seconds
  110.     print()
  111.     print("When you are ready, press Ctrl + C")
  112.     print()
  113.     print("Your Time Starts...")
  114.     time.sleep(2)
  115.     print("Now")
  116.     try:
  117.         for i in range(0,31): #start the loop of 30 seconds
  118.             time.sleep(1) #add each second to the loop
  119.             print(i)
  120.         print("You have run out of time!") #the user has run out of time
  121.         time.sleep(3)
  122.         leave_game() # leave the game
  123.     except KeyboardInterrupt: #if the user has interrupted
  124.         user_guess = input("Please Enter Your Guess: ") #ask for the users guess
  125.     if user_guess == "":
  126.         valid = False #if the answer is blank return an error and repeat input request
  127.         print("Answer cannot be blank")
  128.     if user_guess in word_list: #check if the word is an english word
  129.         user_guess = list(user_guess)
  130.         for i in range(0,len(user_guess)):
  131.             if user_guess[i] not in test_letters: # check if the letters used are in the selection available
  132.                 print("Cannot be made using the given characters") #if not tell the user cannot be made
  133.                 valid = False
  134.                 break
  135.             elif user_guess.count(user_guess[i]) > test_letters.count(user_guess[i]): #check if the chars have been used too many times
  136.                 print("You have used a character too many times!") #if yes tell user
  137.                 valid = False
  138.                 break
  139.             else:
  140.                 valid = True
  141.         if valid == True:
  142.             print("Your word is valid!") # if all conditions are met tell user the word is valid
  143.             print("You scored",len(user_guess),"points.") # find the points made from the users guess
  144.     else:
  145.         print("Not a word in the list of words!")
  146.  
  147.  
  148.    
  149.     get_better_answer(word_list,test_letters) #call to find better answers
  150.  
  151.  
  152.    
  153.  
  154. def main(): #main function called
  155.     print("   _____                  _   _____                      ")
  156.     time.sleep(0.25)
  157.     print("  / ____|                | | |  __ \                    ")
  158.     time.sleep(0.25)
  159.     print(" | |     ___  _   _ _ __ | |_| |  | | _____      ___ __  ")
  160.     time.sleep(0.25)
  161.     print(" | |    / _ \| | | | '_ \| __| |  | |/ _ \ \ /\ / / '_ \ ")
  162.     time.sleep(0.25)
  163.     print(" | |___| (_) | |_| | | | | |_| |__| | (_) \ V  V /| | | |")
  164.     time.sleep(0.25)
  165.     print("  \_____\___/ \__,_|_| |_|\__|_____/ \___/ \_/\_/ |_| |_|") #prints title
  166.     time.sleep(0.25)
  167.     print()
  168.     print("Welcome to Countdown")
  169.     print()
  170.     print("You will be given nine random consonants\nand try to create the largest word possible!")
  171.     print()
  172.     print("Please Select a vowel or consonant (v/c): ") #text to explain what the function is
  173.     word_lookup()
  174.  
  175.  
  176. main() #call main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement