Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #imports
- import secrets
- #load words from a file into a list
- def load_words(filename):
- with open(filename) as f:
- dictionary = f.readlines()
- dictionary = [x.strip() for x in dictionary]
- return dictionary
- #spit out something like c_t for cat and the guessed letters c,t
- def string_underscored(word, letters):
- underscored=[]
- for i in range(0,len(word)):
- if word[i] in letters:
- underscored.append(word[i])
- else:
- underscored.append('_')
- string_underscored=''.join(str(x) for x in underscored)
- return string_underscored
- dictionary=list(load_words('dictionary.txt'))
- #check if all letters of a word are in a string of letters
- def check_if_word_in_letters(word, letters):
- if set(word) <= set(letters): # <= means "is subset" in this context
- return True
- else:
- return False
- #replaying loop
- answer=''
- while answer!='n':
- word_to_guess=secrets.choice(dictionary)
- guessed_letters=''
- underscore_word=string_underscored(word_to_guess, guessed_letters)
- failed_tries=0
- check=0
- #round loop
- while not check and failed_tries<8:
- print('\n'*50)
- print (underscore_word)
- print('Guessed letters:')
- print('failed attempts: ')
- print(failed_tries)
- print(guessed_letters)
- some_letter = input('Guess a letter: ')
- guessed_letters+=some_letter
- underscore_word=string_underscored(word_to_guess, guessed_letters)
- check=check_if_word_in_letters(word_to_guess, guessed_letters)
- if check:
- print('Congratulations!')
- if some_letter not in word_to_guess:
- failed_tries+=1
- print('The word was ' + ''.join(word_to_guess) + '!')
- answer=input('Again? y/n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement