64
loading...
This website collects cookies to deliver better user experience
Now, this is isn't a GUI based game - just a terminal based I/O game that can improve your pythonic knowledge.
Pick a random word from a large dataset.
initialize points to 0
while True:
set POSSIBLE_ALPHABETS = list of "abcdefghijklmnopqrstuvwxyz"
set RANDOM_ALPHABETS = pick 3 random alphabets from POSSIBLE_ALPHABETS
for ALPHABET in RANDOM_ALPHABETS:
replace ALPHABET from the random word with a "_"
print the random word (after replacement of alphabets)
take input from the user as ANSWER
if ANSWER is equal to the random word:
#Hurray!
increment points by 1
else:
#Alas!
print the total points
break
import random
import colorama
from nltk.corpus import brown
Note: Here I'm using brown dataset from nltk.corpus which is a large collection of words - alternatively, you can replace the usage of Brown with your own dataset.
word_list = brown.words()
Note: If you are using your own dataset, try this: word_list = ['apple', 'computer', 'banana', 'soap', 'snake', ...]
0
.points = 0
colorama.init(autoreset=True)
while True:
#rest of the code goes here.
words
:words = word_list
possible_alphabets = list("abcdefghijklmnopqrstuvwxyz")
old_word = random.choice(words)
Note: I also added a variable old_word
to choose a random word from word_list
- I named it like that, just so that I can differentiate it from the word after that it has been modified.
alphabets = [] #initialize alphabets list
for i in range(3):
alphabets += random.choice(possible_alphabets)
alphabets
list, and that I append 3 random letters to the alphabets
list from the possible_alphabets
list (as per the pseudocode we designed before).old_word
with an underscore, so that we could make our guessable string (Eg: "developer" as "de_e_ope_")for alphabet in alphabets:
word = old_word.replace(alphabet, '_')
if "_" in word: #additional error checking
print(word) #printing the word with underscore
answer = input('word: ')
else:
continue
if answer == old_word:
points += 1
print(f'That\'s correct! Total points: {colorama.Fore.CYAN}{points}')
else:
print(f'Sorry, but you lost. (The actual word was {colorama.Fore.GREEN}{old_word}{colorama.Fore.WHITE})\n')
print('------GAME OVER------')
print(f'\nTotal points: {colorama.Fore.CYAN}{points}\n')
break
import random
import colorama
from nltk.corpus import brown
word_list = brown.words()
#word_set = set(word_list)
#print(random.choice(word_list))
points = 0
colorama.init(autoreset=True)
while True:
words = word_list
possible_alphabets = list("abcdefghijklmnopqrstuvwxyz")
old_word = random.choice(words)
alphabets = []
for i in range(3):
alphabets += random.choice(possible_alphabets)
for alphabet in alphabets:
word = old_word.replace(alphabet, '_')
if "_" in word:
print(word)
answer = input('word: ')
else:
continue
if answer == old_word:
points += 1
print(f'That\'s correct! Total points: {colorama.Fore.CYAN}{points}')
else:
print(f'Sorry, but you lost. (The actual word was {colorama.Fore.GREEN}{old_word}{colorama.Fore.WHITE})\n')
print('------GAME OVER------')
print(f'\nTotal points: {colorama.Fore.CYAN}{points}\n')
break
Having any doubts or queries regarding the topic? Feel free to mention it in the comments below and I will try my best to respond to you!