I am trying to make a random word/phrase generator that is like the one that bitwarden has (in python3). But the issue I am running into and need some help with is the addition of 1 number at the end of 1 of the words that is shown.
Something like this Referee-Outrank-Cymbal-Cupping-Cresting-Fiber7-Expensive-Myth-Unveiling-Grasp-Badland-Epiphany-Simplify-Munchkin-Pastrami-Spiffy-Gladly-Skeptic-Retouch-Buckskin
What is very important here is that the number is "random" and the word it is attached to is "random".
Code I have written so far:
Word list I am using is https://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain but without ' in any of the words.
#pycryptodome==3.15.0
from Crypto.Random import random
import beaupy
import os
def clear():
os.system('clear||cls')
def main():
while True:
try:
number = int(beaupy.prompt("How many words?: "))
except ValueError as e:
print(f'Oops! Something went wrong.\nError: {e}\n\n')
input('Press "enter" to continue...')
clear()
continue
if number > 20 or number < 3:
print("20 words is the maximum number of words you can use. And 5 words is the minimum.\n\n")
input('Press "enter" to continue...')
clear()
else:
break
cwd = os.getcwd()
word_path = f"{cwd}/words.txt"
with open(word_path, 'r') as fh:
words = fh.read().lower()
word_list = words.splitlines() #list of words
sep = beaupy.prompt('Line separator? (leave empty for default "-"): ')
if sep == '' or sep == ',':
sep = '-'
#Returns True or False. Basically Yes or No?
if beaupy.confirm("Capitalize?"):
"""Make list of words with the first letter capitalized."""
c_lst = []
for i in word_list:
c_lst.append(i.title())
capital_words = f'{sep}'.join(random.choice(c_lst) for _ in range(number))
else:
default_words = f'{sep}'.join(random.choice(word_list) for _ in range(number))
if beaupy.confirm("Number?"):
rn_num = random.randint(0, 9) # <-- Get a random number to be used with only 1 of the words defined in capital_words or default_words below.
#I don't know what to do here... but I need to have a version with the number and one without. (default)
if __name__ == '__main__':
clear()
main()
I am not exactly familiar with string manipulation and searching for answers online just isn't giving me any help with the very specific thing I'm trying to do. All I want is for 1 word in the resulting string to have a "random" number attached to it.
I don't know if I need to re order my code and have it be done a different way. I am having such a headache with this. Any help would be great.
Edit#1
Additional and unrelated note, If anyone knows of a better word list to use, please let me know!
If I am understanding correctly, here is a solution:
#Returns True or False. Basically Yes or No?
capital_words = ''
default_words = ''
if beaupy.confirm("Capitalize?"):
"""Make list of words with the first letter capitalized."""
c_lst = []
for i in word_list:
c_lst.append(i.title())
capital_words = f'{sep}'.join(random.choice(c_lst) for _ in range(number))
else:
default_words = f'{sep}'.join(random.choice(word_list) for _ in range(number))
if beaupy.confirm("Number?"):
rn_num = random.randint(0, 9) # <-- Get a random number to be used with only 1 of the words defined in capital_words or default_words below.
#I don't know what to do here... but I need to have a version with the number and one without. (default)
word_index = random.randint(0, number - 1) # Get random index that is in the word list
if default_words != '':
word_with_number = default_words.split(sep)
else:
word_with_number = capital_words.split(sep)
word_with_number[word_index] = word_with_number[word_index] + str(rn_num)
word_with_number = sep.join(word_with_number)
print(word_with_number)
if default_words != '':
print(default_words)
else:
print(capital_words)
OUTPUT:
detroit-elide-windbag-purge-tort-mortician-codex7-annex-fairy-suntanning
detroit-elide-windbag-purge-tort-mortician-codex-annex-fairy-suntanning
With some help from AnonymousFrog. I was able to get my code working.
The following is the now working code.
from Crypto.Random import random
import beaupy
import os
def clear():
os.system('clear||cls')
def main():
while True:
try:
number = int(beaupy.prompt("How many words?: "))
except ValueError as e:
print(f'Oops! Something went wrong.\nError: {e}\n\n')
input('Press "enter" to continue...')
clear()
continue
if number > 20 or number < 3:
print("20 words is the maximum number of words you can use. And 5 words is the minimum.\n\n")
input('Press "enter" to continue...')
clear()
else:
break
cwd = os.getcwd()
word_path = f"{cwd}/words.txt"
with open(word_path, 'r') as fh:
words = fh.read().lower()
word_list = words.splitlines() #list of words
sep = beaupy.prompt('Line separator? (leave empty for default "-"): ')
if sep == '' or sep == ',':
sep = '-'
#Returns True or False. Basically Yes or No?
capital_words = ''
default_words = ''
if beaupy.confirm("Capitalize?"):
"""Make list of words with the first letter capitalized."""
c_lst = []
for i in word_list:
if len(i) < 3 or len(i) > 9:
pass
else:
c_lst.append(i.title())
cap = True
capital_words = f'{sep}'.join(random.choice(c_lst) for _ in range(number))
else:
cap = False
default_words = f'{sep}'.join(random.choice(word_list) for _ in range(number))
if beaupy.confirm("Number?"):
num = True
rn_num = random.randint(0, 9) # <-- Get a random number to be used with only 1 of the words defined in capital_words or default_words below.
word_index = random.randint(0, number - 1) # Get random index that is in the word list
if default_words != '':
word_with_number = default_words.split(sep)
else:
word_with_number = capital_words.split(sep)
word_with_number[word_index] = word_with_number[word_index] + str(rn_num)
word_with_number = sep.join(word_with_number)
else:
num = False
if cap == True and num == False:
print(capital_words)
if cap == False and num == False:
print(default_words)
if num == True:
print(word_with_number)
Thanks for the help!
(if anyone knows of a better word list to use, feel free to let me know)
I am attempting to write a program in python that simulates a game of cheating hangman, but am receiving positional argument errors upon compilation. I wrote the class and methods in hangman.py and called the play method in play_hangman.py. When I attempt to run it, it gives the following error:
play() missing 9 required positional arguments: 'askForWordLength', 'askForNumberOfGuesses', 'remainingWords', 'words', 'wordStatus', 'printCountOfRemainingWords', 'printGameStats', 'askPlayerForGuess', and 'retrieveRemainingWords'
play_hangman.py
from hangman import Hangman
game = Hangman()
game.play()
y = input("Would you like to play again? yes or no: ")
while y == 'yes':
game.play()
y = input("Would you like to play again? yes or no: ")
hangman.py
import re
class Hangman:
# hangman self method
def hangman(self):
self.hangman = Hangman() # object of the Hangman class
def words(self):
with open('dictionary.txt') as file: # opens dictionary text file
file_lines = file.read().splitlines() # reads and splits each line
all_words = [] # empty list to contain all words
valid_words = [] # empty list to contain all valid words
for word in file_lines: # traverses all words in the file lines
if len(word) >= 3: # accepts word if it has at least 3 letters
all_words.append(word) # appends accepted word to list
# list of all invalid characters in python
CHARACTERS = ["~", "`", "!", "#", "#", "$", "%", "^", "&", "*", "(",
")", "-", "_", "=", "+", "[", "]", "{", "}", "|", "\","
"", "'", "?", "/", ">", ".", "<", ",", "", ";", ":"]
for i in CHARACTERS: # traverse list of invalids
for word in all_words:
if i not in word: # if invalid character is not in word
valid_words.append(word) # accept and append to list
return valid_words # return list of valid words
def askForWordLength(self, valid_words):
word_lengths = [] # empty list for possible word lengths
for word in valid_words: # traverse list of valid words
length = word.__len__() # record length of current word
if (length not in word_lengths):
word_lengths.append(length) # accept and append to list
word_lengths.sort()
# inform user of possible word lengths
print('The available word lengths are: ' + str(word_lengths[0]) + '-'
+ str(word_lengths[-1]))
print()
# have user choose from possible word lengths
while(1):
try:
length = int(input('Please enter the word length you want: '))
if (length in word_lengths):
return length
except ValueError:
print('Your input is invalid!. Please use a valid input!')
print()
def askForNumberOfGuesses(self):
while(1):
try:
num_guesses = int(input('Enter number of guesses you want: '))
if (num_guesses >= 3):
return num_guesses
except ValueError:
print('Your input is invalid!. Please use a valid input!')
print()
def wordStatus(self, length):
status = '-'
for i in range(0, length):
status += '-'
return
def remainingWords(self, file_lines, length):
words = []
for word in file_lines:
if (word.__len__() == length):
words.append(word)
return words
def printGameStats(self, letters_guessed, status, num_guesses):
print('Game Status: ' + str(status))
print()
print('Attempted Guesses' + str(letters_guessed))
print('Remaining Guesses' + str(num_guesses))
def askPlayerForGuess(self, letters_guessed):
letter = str(input('Guess a letter: ')).lower()
pattern = re.compile("^[a-z]{1}$")
invalid_guess = letter in letters_guessed or re.match(pattern, letter) == None
if (invalid_guess):
while (1):
print()
if (re.match(pattern, letter) == None):
print('Invalid guess. Please enter a correct character!')
if (letter in letters_guessed):
print('\nYou already guessed that letter' + letter)
letter = str(input('Please guess a letter: '))
valid_guess = letter not in letters_guessed and re.match(pattern, letter) != None
if (valid_guess):
return letter
return letter
def retrieveWordStatus(self, word_family, letters_already_guessed):
status = ''
for letter in word_family:
if (letter in letters_already_guessed):
status += letter
else:
status += '-'
return status
def retrieveRemainingWords(self, guess, num_guesses, remaining_words,
wordStatus, guesses_num, word_length,
createWordFamiliesDict,
findHighestCountWordFamily,
generateListOfWords):
word_families = createWordFamiliesDict(remaining_words, guess)
family_return = wordStatus(word_length)
avoid_guess = num_guesses == 0 and family_return in word_families
if (avoid_guess):
family_return = wordStatus(word_length)
else:
family_return = findHighestCountWordFamily(word_families)
words = generateListOfWords(remaining_words, guess, family_return)
return words
def createWordFamiliesDict(self, remainingWords, guess):
wordFamilies = dict()
for word in remainingWords:
status = ''
for letter in word:
if (letter == guess):
status += guess
else:
status += '-'
if (status not in wordFamilies):
wordFamilies[status] = 1
else:
wordFamilies[status] = wordFamilies[status] + 1
return wordFamilies
def generateListOfWords(self, remainingWords, guess, familyToReturn):
words = []
for word in remainingWords:
word_family = ''
for letter in word:
if (letter == guess):
word_family += guess
else:
word_family += '-'
if (word_family == familyToReturn):
words.append(word)
return words
def findHighestCountWordFamily(self, wordFamilies):
familyToReturn = ''
maxCount = 0
for word_family in wordFamilies:
if wordFamilies[word_family] > maxCount:
maxCount = wordFamilies[word_family]
familyToReturn = word_family
return familyToReturn
def printCountOfRemainingWords(self, remainingWords):
show_remain_words = str(input('Want to view the remaining words?: '))
if (show_remain_words == 'yes'):
print('Remaining words: ' + str(len(remainingWords)))
else:
print()
def play(self, askForWordLength, askForNumberOfGuesses, remainingWords,
words, wordStatus, printCountOfRemainingWords, printGameStats,
askPlayerForGuess, retrieveRemainingWords):
MODE = 1
openSession = 1
while (openSession == 1):
word_length = askForWordLength(words)
num_guesses = askForNumberOfGuesses()
wordStatus = wordStatus(word_length)
letters_already_guessed = []
print()
game_over = 0
while (game_over == 0):
if (MODE == 1):
printCountOfRemainingWords(remainingWords)
printGameStats(remainingWords, letters_already_guessed,
num_guesses, wordStatus)
guess = askPlayerForGuess(letters_already_guessed)
letters_already_guessed.append(guess)
num_guesses -= 1
remainingWords = retrieveRemainingWords(guess, remainingWords,
num_guesses, word_length)
wordStatus = wordStatus(remainingWords[0], letters_already_guessed)
print()
if (guess in wordStatus):
num_guesses += 1
if ('-' not in wordStatus):
game_over = 1
print('Congratulations! You won!')
print('Your word was: ' + wordStatus)
if (num_guesses == 0 and game_over == 0):
game_over = 1
print('Haha! You Lose')
print('Your word was: ' + remainingWords[0])
print('Thanks for playing Hangman!')```
Your play() method requires many arguments:
def play(
self,
askForWordLength,
askForNumberOfGuesses,
remainingWords,
words,
wordStatus,
printCountOfRemainingWords,
printGameStats,
askPlayerForGuess,
retrieveRemainingWords
):
...
When you call the function, you need to provide all of those arguments, aside from self, otherwise, Python won't know what the value of those variables should be.
Trying to sort out words that do not have the yellow letters (yl).
currently it doesn't require them to have both... I need it to have both
get it to print just [again]
-0
-'g','i','n'
-'o','e','l'
import sys
import time
#list of words that are available
list0 = [
'again',
'alone',
'align',
]
cq = 0
s1 = ''
l1 = 0
dw = ''
yl = ''
print("it's location is -1 to -5 (last to first) or 0 to 4 (first to last)")
print("grey letter format is - '_','_', for every grey letter")
if cq == 0:
s1 = input('what is the green letter?; ')
print()
time.sleep(2)
l1 = input('where is ' + s1 + '?; ')
print()
time.sleep(2)
yl = input('what letters are yellow?; ')
print()
time.sleep
dw = input('what letters are grey?; ')
print()
cq += 1
print("Searching.")
time.sleep(1)
print("Searching..")
time.sleep(1)
print("Searching...")
time.sleep(1)
print()
#letters that can't be used
deadletters = 'm'
list1 = [w for w in list0 if (any(set(w) & set(yl)))]
#sorts out words with dead letters
list2 = [w for w in list1 if not (any(set(w) & set(dw)))]
#only lets words with "a___y" through
found = [w for w in list2 if (w[int(l1)] == s1)]
print()
print(found)
Having Fun Playing Wordle...
don't worry about me...
i need more words cause i have too much code...
:)...
I want to create a hangman game. I want it to keep calling x and printing new_word until there is no "_". I have tried it but the slots keep refreshing. It keeps reprinting. It won't update the value itself.
word = 'EVAPORATE'
wordlist = list(word)
dict = dict(enumerate(wordlist))
slots = list('_' * len(word))
x = input("Guess the letter: ")
def game():
for a,b in dict.items():
if b == x:
slots[a] = x
new_word = ' '.join(slots)
print(new_word)
game()
This seems to work for me:
word = 'EVAPORATE'
wordlist = list(word)
dict = dict(enumerate(wordlist))
slots = list('_' * len(word))
def game():
while '_' in slots:
x = input("Guess the letter: ")
for a,b in dict.items():
if b == x.upper():
slots[a] = x
new_word = ' '.join(slots)
print(new_word)
game()
I have added in a while loop just inside def game(): so that the code will keep running until slots has no underscores left in it. I then moved x = input("Guess the letter: " to inside the while loop so the user can always have another guess until the word is completed.
A few things to add:
Don't you ever use keywords such as list or dict for variables
You have to count the matchs for each letter, example E appears twice, so, you have to count it twice
You have to know when the game ends, because you want to loop the question "guess letter" until the game ends
Add a While loop
Enjoy your game
word = 'EVAPORATE'
wordlist = list(word)
word_length = len(word)
word_dict = dict(enumerate(wordlist))
slots = list('_' * len(word))
def game():
total_letters = word_length
while not game_ended(total_letters):
x = input("Guess the letter: ")
matchs = 0
for pos,letter in word_dict.items():
if letter == x:
matchs += 1
slots[pos] = x
new_word = ' '.join(slots)
total_letters -= matchs
print(new_word)
def game_ended(word_len):
return word_len == 0
game()
just put everything from the input until the end in a while loop
word = 'EVAPORATE'
wordlist = list(word)
# it is not a good idea name a variable the same as a builtin, so change from 'dict' to 'worddict'
worddict = dict(enumerate(wordlist))
slots = list('_' * len(word))
def game(x):
# we also need to change it here
for a,b in worddict.items():
if b == x:
slots[a] = x
new_word = ' '.join(slots)
print(new_word)
while any([i == '_' for i in slots]):
x = input("Guess the letter: ")
game(x)
the while check if any letter in slots is a _, if there is any, keep playing.
note that I also pass the input as a variable to game (it's not necessary, but it's better)
So I'm doing hangman on python, and I was wondering how I would mark a letter incorrect if it has already been used.
def __init__(self):
self.wordLetter = []
self.binaryWord = []
self.wordLength = 0
self.numberCorrect = 0
self.numberIncorrect = 0
def secretWord(self):
self.numberCorrect = 0
self.numberIncorrect = 0
self.wordLetters = ['f','o','o','t','b','a','l','l']
self.wordLength = len(self.letterWord)
for val in range(0,self.wordLength):
self.binaryWord.append(0)
def checkLetter(self, letter):
for val in range(len(self.wordLetter)): #checks if letter is correct and takes appropriate action
if self.wordLetter[val] == letter:
self.binaryWord[val] = 1
self.numberCorrect += 1
print "CORRECT!!"
if letter not in self.wordLetters:
self.numberIncorrect += 1
print "Incorrect letter. Try again"
If the word is "football," how would I make it so if I input the letters f,o,t,b,a, or l more than once, it will come up as incorrect?
keep track of the letters the user has already submitted
def __init__(self):
...
...
self.submittedLetters[]
...
...
Then in your check letter check and see if the letter is already in the list of letters
if letter in self.submittedLetters:
#already entered
else:
#add letter to self.submittedLetters
...
#the rest of your method
You can save in one variable word and in another a set of this word
self.word = 'football'
self.wordLatters = set(self.word)
And check letter like this
def checkLetter(self, letter):
if letter in self.wordLatters:
self.binaryWord[val] = 1
self.numberCorrect += 1
self.wordLatters.remove(letter)
print "CORRECT!!"
else:
self.numberIncorrect += 1
print "Incorrect letter. Try again"