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)
Related
Im trying to put hint in output like this >>
--e - - ca
p--i--a-
how can i?
import random
word_list = ["india", "pakistan", "america"]
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
display = []
for _ in range (word_length):
letter = chosen_word[_]
display += '_'
print(display)
lives = 8
game_over = False
while not game_over:
guess = input("enter a guess:").lower()
for position in range(word_length):
random.choice(display[position])
letter = chosen_word[position]
if letter == guess:
display[position] = letter
if guess not in chosen_word:
lives -= 1
if lives == 0:
If you always want your hint to contain 3 letters and the letters should be random, you could do:
hint = ["-"]*word_length
# Gets 3 random positions to have for the letters
positions = random.sample(range(0,word_length), 3)
# At those positions, change the "-" in hint to a letter.
for i in positions:
hint[i] = chosen_word[i]
hint = "".join(hint)
print(hint)
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.
I am using CodeHS for my Computer Science Principles class and one of the problems in the Strings section is really confusing me. We have to remove all of one string from another string.
These are the official instructions:
Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string with all instances of the second string removed. You can assume that the second string is only one letter, like "a".
We are required use:
A function definition with parameters
A while loop
The find method
Slicing and the + operator
A return statement
We are expected to only have to use those 5 things to make it work.
I attempted to write this program but my function doesn't do anything and I am really stumped.
def remove_all_from_string(word, letter):
while letter in word:
x=word.find(letter)
if x==-1:
continue
else:
return x
print word[:x] + word[x+1:]
remove_all_from_string("alabama", "a")
The easiest way to do this would obviously just be
def remove_all_from_string(word, letter):
return word.replace(letter, "")
However, considering the parameters, another way we could do this is like so:
def remove_all_from_string(word, letter):
while letter in word:
x=word.find(letter)
if x == -1:
continue
else:
word = word[:x] + word[x+1:]
return word
You could run this and print it by typing
>>> print(remove_all_from_string("Word Here", "e"))
#returns Word hr
def remove_all_from_string(word, letter):
while letter in word:
x=word.find(letter)
if x == -1:
continue
else:
word = word[:x] + word[x+1:]
return word
print(remove_all_from_string("hello", "l"))
def remove_all_from_string(word, letter):
letters = len(word)
while letters >= 0:
x=word.find(letter)
if x == -1:
letters = letters - 1
continue
else:
# Found a match
word = word[:x] + word[x+1:]
letters = letters - 1
return word
remove_all_from_string("alabama", "a")
I have this so far and it keeps saying that message is not defined and when I define it with find_secret_word it says "find_secret_word" is not defined, what do I do?
This is my code:
`word = "bananas"
letter = "na"
index = word.find(letter)
def remove_all_from_string(word, letter):
while letter in word:
x=word.find(letter)
if x == -1:
continue
else:
word = word[:x] + word[x+1:]
return word
word = word[:index] + word[index+len(letter):]
print(remove_all_from_string("hello", "l"))
def find_secret_word(message):
while True:
return hidden_word
hidden_word = "iCjnyAyT"
for letter in message:
if letter.upper():
hidden_word = hidden_word + letter
print (find_secret_word(message))`
secret_word = "python"
correct_word = "yo"
count = 0
for i in secret_word:
if i in correct_word:
print(i,end=" ")
else:
print('_',end=" ")
so the outcome of the code will look like this _ y _ _ o _
my question is how i can i get the same output by using while loop instead of using For loop. i know i have to use index to iterate over each character but when i tried i failed . so any help?
while count < len(secret_word):
if correct_word [count]in secret_word[count]:
print(correct_word,end=" ")
else:
print("_",end=" ")
count = count + 1
Thanks
You can do this:
secret_word = "python"
correct_word = "yo"
count = 0
while count < len(secret_word):
print(secret_word[count] if secret_word[count] in correct_word else '_', end=" ")
count += 1
Another way to use while is to simulate a pop of the first character. The while loop terminates when the 'truthiness' of a string becomes false with no more characters to process:
secret_word = "python"
correct_word = "yo"
while secret_word:
ch=secret_word[0]
secret_word=secret_word[1:]
if ch in correct_word:
print(ch,end=" ")
else:
print('_',end=" ")
Or, you can actually use a list with a LH pop:
secret_list=list(secret_word)
while secret_list:
ch=secret_list.pop(0)
if ch in correct_word:
print(ch,end=" ")
else:
print('_',end=" ")
Here is a simple, way of writing your program with a while loop instead of a for loop. The code breaks out of an infinite loop when appropriate.
def main():
secret_word = 'python'
correct_word = 'yo'
iterator = iter(secret_word)
sentinel = object()
while True:
item = next(iterator, sentinel)
if item is sentinel:
break
print(item if item in correct_word else '_', end=' ')
if __name__ == '__main__':
main()
It uses logic similar to how the for loop is implemented internally. Alternatively, the example could have used exception handling instead.
I apologize if this is just a dumb slip-up on my part, but I am relatively inexperienced with Python and I can't figure out why this isn't working.
I have a class called Game, which contains a word_list that has been read in from a text file. One of the method of the class is as follows:
def make_guess(self, guess):
print("Guess: ", guess)
if guess == self.target_word:
print("Got it!")
if guess in self.word_list:
num_right = self.compare_letters(guess, self.target_word)
else:
print("Not valid guess; not in list")
No matter what input I give it, I can never make it trip the if guess in self.word_list path. I tried comparing the type of the variables (each word in the list, and my input), but they appeared to be the same to me.
The whole definition of the class if it helps:
class Game:
def __init__(self, difficulty):
self.difficulty = difficulty
if 0 < difficulty < 3:
self.remaining_guesses = 5
elif 3 <= difficulty < 5:
self.remaining_guesses = 4
else:
self.remaining_guesses = 3
self.word_list = []
self.dictionary = open("wordsEn.txt")
for word in self.dictionary:
percent = int(floor(1000*random()))
if len(word) == 6 and percent < 2:
self.word_list.append(word)
self.dictionary.close()
percent = int(floor(len(self.word_list)*random()))
self.target_word = self.word_list[percent]
def make_guess(self, guess):
print("Guess: ", guess)
if guess == self.target_word:
print("Got it!")
if guess in self.word_list:
num_right = self.compare_letters(guess, self.target_word)
else:
print("Not valid guess; not in list")
def display_word_list(self):
print("in display")
print(self.remaining_guesses)
for word in self.word_list:
print(word)
print("Target: ", self.target_word)
def compare_letters(self, guess, target_word):
for letter in guess:
if letter == letter:
print("yes")
`
In main, I have:
new_game = Game(difficulty)
guess = input("Guess: ")
new_game.make_guess(guess)
Even if I deliberately guess a word that I know to be in the list, it never says that the word is in fact in the list. What stupid mistake am I making? (and if you could point out ways I could adhere more to the Python style, that would be appreciated as well!)
You need to strip newlines from lines of wordsEn.txt. After
for word in self.dictionary:
insert:
word = word.rstrip()
I'm assuming that each line of wordsEn.txt lists a single word.
Instead of adding the full line to self.word_list, add each word by calling self.dictionary = open("wordsEn.txt").read().split() instead. Here is your edited class:
class Game:
def __init__(self, difficulty):
self.difficulty = difficulty
if 0 < difficulty < 3:
self.remaining_guesses = 5
elif 3 <= difficulty < 5:
self.remaining_guesses = 4
else:
self.remaining_guesses = 3
self.word_list = []
self.dictionary = open("wordsEn.txt").read().split()
for word in self.dictionary:
percent = int(floor(1000*random()))
if len(word) == 6 and percent < 2:
self.word_list.append(word)
self.dictionary.close()
percent = int(floor(len(self.word_list)*random()))
self.target_word = self.word_list[percent]
def make_guess(self, guess):
print("Guess: ", guess)
if guess == self.target_word:
print("Got it!")
if guess in self.word_list:
num_right = self.compare_letters(guess, self.target_word)
else:
print("Not valid guess; not in list")
def display_word_list(self):
print("in display")
print(self.remaining_guesses)
for word in self.word_list:
print(word)
print("Target: ", self.target_word)
def compare_letters(self, guess, target_word):
for letter in guess:
if letter == letter:
print("yes")
Demonstrating the above concept of .read().split():
>>> file = open('blah.txt')
>>> for word in file:
... word
...
'Hello,\n'
'\n'
'These\n'
'Are\n'
'Some\n'
'Vocabulary\n'
'Words\n'
'\n'
'Regards,\n'
'Me\n'
>>> file = open('blah.txt').read().split()
>>> for word in file:
... word
...
'Hello,'
'These'
'Are'
'Some'
'Vocabulary'
'Words'
'Regards,'
'Me'
>>>
In the line where you say
for word in self.dictionary:
This reads an entire line from the text file. So the variable word doesn't refer to a word in the text file. You should first read a line from the text file and then take individual words from the line. Like this :
for line in self.dictionary:
words=line.split();
for word in words: