So my program is this so far:
def update():
print word
counter = 0
blanks = len(word)*'-'
blank_list = list(blanks)
letter = raw_input('Please enter a single letter: ')
for index in range(len(word)):
if letter in word[index]:
blank_list[index] = letter
print ''.join(blank_list)
letter = raw_input('Please enter a single letter: ')
but when I enter a correct letter in the wrong order it displays it as an incorrect guess, ie, one of my words is horse, if i entered an o it would display -o---, but if I entered an h, it would come up as None, because haven't set parameters of what to do when it is incorrect guess. This goes for when it is if letter in word[index] or if letter == word[index].
any suggestions on how to fix this problem?
Here, you enforce order by looking at the exact index:
if letter == word[index]
You want to ask if letter in word. You can also find the exact index of the letter with word.index(letter) (careful with repeated letters).
I think the problem is that you have a for loop based on index so when you enter a correct answer the next correct letter has to be in a later index because you already passed the previous indexes. For a hangman program I might do something with a while loop and use a count like you have and if the count exceeds your limit then break the while loop and make you word a list of letters. Then check to see if your guessed letter is within your list of letters of the word.
so if your guess limit is 5 then do something like
break word into list
set guess count to 0
while guess_count < 5:
if letter in word_list:
#update
else:
guess_count += 1
The problem is that your guesses are based on indexing. Once you guess a letter, you cannot guess one before that.
Running your code:
>>> update('mouse')
mouse
Please enter a single letter: m
m----
Please enter a single letter: u
m-u--
Please enter a single letter: o
>>>
It exits because the placing of 'u' is after that of 'o'. Instead change your code to this:
def update(word):
found = False
guesses_left = 3
blank_list = list(len(word)*'_')
while guesses_left > 0:
letter = raw_input('Enter your letter: ')
for k in range(len(word)):
if word[k] == letter and letter not in blank_list:
blank_list[k] = letter
found = True
if found == False:
guesses_left-=1
else:
found = False
print ''.join(blank_list)
if '_' not in blank_list:
break
if guesses_left < 1:
print 'You lost!'
else:
print 'You won!'
Running my code:
>>> update('mouse')
Enter your letter: m
m____
Enter your letter: s
m__s_
Enter your letter: u
m_us_
Enter your letter: o
mous_
Enter your letter: e
mouse
You won!
>>>
Related
Hangman. As you probobly understand i am new to coding and python, sorry for bad code.
The best way i can think of to describe this problem is through the following way: In the "try:" section i try to index = list_of_letters.index(guesssed_letter_string). i want to check if the guessed_letter_string is in list_of_letters: i earlier in the code translate the input from well the only input() in the code to guessed_letter_string (its the same thing). when you input a letter in the middel of the word it works like index[3] but when you input the first letter in the word the index locks at 0 and every other letter replaces it. is there a way to fix this
import random
list_of_words = ["mom", "dad", "sister", "brother"]
random_word = random.choice(list_of_words)
list_of_letters = list(random_word)
print(random_word)
print(list_of_letters)
rounds_failed = 1
rounds_max = 16
list_of_letters_guessed = []
under_grejer = []
count_of_right_letters_list = []
print(f"You have {rounds_max - rounds_failed} rounds left to find out the word")
for every in list_of_letters:
under_grejer.extend("_")
while True:
if rounds_failed == rounds_max:
print("To many attempts, no more rounds")
break
if len(list_of_letters) == 0:
print("YOU FUCKING WON")
break
print(f"This is round: {rounds_failed}")
print(" ".join(under_grejer))
print("Letters that are correct(not in order): "+", ".join(count_of_right_letters_list))
print("List of letters guessed: "+", ".join(list_of_letters_guessed))
guess = input("NAME A Letter: ")
guess_letters_list = (list(guess))
guess_count_letters = len(guess_letters_list)
if guess_count_letters > 1:
print("Dummy you just need to input a letter, nothing else")
guesssed_letter_string = " ".join(guess_letters_list)
try:
index = list_of_letters.index(guesssed_letter_string)
print("Congrats you got the letter: " + guesssed_letter_string)
print(f"thats the {index + 1}nd letter in the word")
rounds_failed += 1
count_of_right_letters_list.extend(guesssed_letter_string)
print(index)
list_of_letters.pop(index)
under_grejer[index] = guesssed_letter_string
except ValueError:
print("try again mate that letter was not in the word")
list_of_letters_guessed.append(guesssed_letter_string)
rounds_failed += 1
continue
It's not about the first letter only. Your problem is that with list_of_letters.pop(index) you remove the guessed letter form list_of_letters; parts of your code rely on this to check if you guessed all occurrences of that letter before already, but on the other hand this reduces the index of all letters behind the guessed one for later iterations.
For example, for brother, if you guess r it correctly says position 2, but if you then guess o next, it again says position 2 because your list_of_letters now reads ["b","o","t","h","e","r"].
You could either try to work with list_of_letters_org = list_of_letters.copy() which you will never change, and pick the right one for every task, or you could for example change the program structure by adding a list of booleans that store which letters were guessed already.
I want to create the hangman python game, so I created a simpler piece of the game. The idea of the game is to guess the letters in a randomly generated word. Here is what I have so far. I listed the problem and questions I have at the bottom.
import random
words = ['holiday', 'american', 'restaurant', 'computer', 'highschool', 'programming']
random_word = random.choice(words)
count = 0
while True: #repeatedly asks you to guess the letter in a random word
guess = str(input("Guess a letter: "))
guess = guess.lower()
if guess in random_word: #checks if the letter you input is in the random generated word
print("Yay, its in the word")
else:
count += 1
print("Not in the word, attempts: %d" % count)
if count > 5:
print("You have reached max attempts")
print("Sorry, but hangman died! You lose")
break
else:
continue
The problem I have: When the user guesses a letter, they can guess it again infinite times. How can I make it so that the user can't repeatedly guess the same letter?
Is there a way to make sure the user doesn't guess the same letter? This could be a problem in an actual hangman game when there are several letters that are the same. Any help/feedback appreciated, thanks!
Here is one way of doing it.
import random
words = ['holiday', 'american', 'restaurant', 'computer', 'highschool', 'programming']
random_word = random.choice(words)
count = 0
guess_list = []
while True: #repeatedly asks you to guess the letter in a random word
guess = str(input("Guess a letter: "))
guess = guess.lower()
if guess not in guess_list:
if guess in random_word: #checks if the letter you input is in the random generated word
print("Yay, its in the word")
else:
count += 1
print("Not in the word, attempts: %d" % count)
if count > 5:
print("You have reached max attempts")
print("Sorry, but hangman died! You lose")
break
else:
print("You have already guessed {}. Try again".format(guess))
print(set(guess_list))
guess_list.append(guess)
Sample output (word is computer):
Guess a letter: c
Yay, its in the word
Guess a letter: e
Yay, its in the word
Guess a letter: e
You have already guessed e. Try again
{'e', 'c'}
Guess a letter: e
You have already guessed e. Try again
{'e', 'c'}
Guess a letter: w
Not in the word, attempts: 1
Guess a letter: m
Yay, its in the word
Guess a letter: m
You have already guessed m. Try again
{'w', 'm', 'e', 'c'}
Guess a letter:
Notes:
A guess_list is created that keeps record of all the guesses.
After each guess the letter is appended to the list.
When the user repeats the guess they are warned. We use a set so only the unique elements in the guess list are displayed.
The code can be refined further, but must do the job.
Depending on the word, you may need to allow multiple guesses if the letter appears more than once. (this may not be your requirement. Not familiar with the game.)
Approach
What you need is a store that keeps track of the number of occurrences of each letter in the word.
Something like:
letterCounts = Counter("hello") # word: 'hello' => {'h': 1, 'e': 1, 'l': 2, 'o': 1}
I'm using the counter collection here.
Then you can decrement the count of the guessed letter
if guesses_letter in list(letterCounts.elements()):
if (letterCounts[guessed_letter] > 0):
letterCounts[guessed_letter] -= 1
else:
# the letter can't be repeated now. do the needful here.
here is my python game:
import random
print('H A N G M A N')
hidden_words = ['python', 'java', 'kotlin', 'javascript']
hidden_word = random.choice(hidden_words)
display = '-' * len(hidden_word)
used = []
i = 0
maxshit = 8
while i < maxshit and display != hidden_word:
print()
print(display)
inp = input("Input a letter: ")
if inp in used:
print("No improvements")
i += 1
elif inp in hidden_word:
new = ""
used.append(inp)
for i in range(len(hidden_word)):
if inp == hidden_word[i]:
new += inp
else:
new += display[i]
display = new
else:
print("No such letter in the word")
i += 1
if i == maxshit:
print("You are hanged!")
else:
print("You survived!")
print()
print('Thanks for playing!')
print("We'll see how well you did in the next stage")
It works well when there is 8 wrong answers you lose and game print "You are hanged !",but if you answer is well you win and it prints "You survived" and if you print allredy printed word it prints "No improvments!" or if there is no word what you inputed it prints "No such letter in the world !".
But when you input looks like
$ python govno.py
H A N G M A N
------
Input a letter: j
No such letter in the word
------
Input a letter: i
----i-
Input a letter: g
No such letter in the word
----i-
Input a letter: g
No such letter in the word
----i-
Input a letter: k
k---i-
Input a letter: g
No such letter in the word
k---i-
Input a letter: g
No such letter in the word
k---i-
Input a letter: g
No such letter in the word
You are hanged!
In this input you lose only after 6 mistakes. Why is this like that?
P.S sorry for comon mistakes I am only begining
The problem is in your second loop (the for loop) you reused the variable i. When the input is in the hidden word and after the loop the variable i will be set to one less than the length of the hidden word ("kotlin"), which in this case is 5. After that, the i was incremented 3 times, ending at the value of 8, which is equal to maxshit. So the loop ended and the message was displayed.
Change that variable to another name (e.g. j).
The loop should look something like this:
for j in range(len(hidden_word)):
if inp == hidden_word[j]:
new += inp
else:
new += display[j]
I am very new to python and I am attempting to make a hangman game.
I would like to change a string to show the number of guessed letters but for some reason I keep on getting weird results. Here is my code:
import random
guesses_left = 9
def show_guesses_left():
print("You have", guesses_left, "guesses left")
wordlist = ['nerd', 'python', 'great', 'happy', 'programmer', 'long', 'short', 'stupid']
word = random.choice(wordlist)
wordwin = word
hidden_word = ["?" for q in word]
letters_guessed = ''.join(hidden_word)
print("Welcome to Hangman!!")
print("My word is", len(word), "letters long")
print(wordwin)
print(letters_guessed)
def request_guess():
global guesses_left
global word
global letters_guessed
x = input(f"What is your guess? \n{letters_guessed}")
if x in word:
print("Great you guessed a letter")
t = word.find(x)
word = word.replace(x, "")
print(t)
letters_guessed = letters_guessed[:t] + letters_guessed[t:t+1].replace('?', x) + letters_guessed[t+1:]
elif type(x) is not str or len(x) > 1:
print("Invalid guess, Your guess must be 1 letter long")
else:
print("Wrong!")
guesses_left -= 1
show_guesses_left()
def start_game():
global letters_guessed
global word
global guesses_left
letters_guessed = ''.join(hidden_word)
while True:
if guesses_left > 0 and len(word) != 0:
request_guess()
elif len(word) == 0:
print(f"YOU WIN!!!, the word was {wordwin}")
break
else:
print("You lose! Better luck next time!")
break
start_game()
I keep on getting this result where it only works for the for some letters and the placing is wrong. Here is my result:
Welcome to Hangman!!
My word is 4 letters long
long
????
What is your guess?
????l
Great you guessed a letter
0
What is your guess?
l???n
Great you guessed a letter
1
What is your guess?
ln??o
Great you guessed a letter
0
What is your guess?
ln??g
Great you guessed a letter
0
YOU WIN!!!, the word was long
Why cant i just slice the string change one character and slice the rest?
Why does it work the first time and not the second?
If anybody can explain to me what is going on it would be appreciated
Solution
import random
guesses_left = 9
def show_guesses_left():
print("You have", guesses_left, "guesses left")
wordlist = ['nerd', 'python', 'great', 'long', 'short', 'stupid', 'happy', 'programmer']
word = random.choice(wordlist)
wordwin = list(word)
hidden_word = list('?' * len(word))
letters_guessed = ''.join(hidden_word)
print("Welcome to Hangman!!")
print("My word is", len(word), "letters long")
print(letters_guessed)
def request_guess():
global guesses_left
global word
global letters_guessed
x = input("\nWhat is your guess?\n" + letters_guessed + "\n")
if x in word:
print("\nGreat you guessed a letter")
for i, j in enumerate(word):
if j == x:
hidden_word[i] = j
letters_guessed = ''.join(hidden_word)
print(letters_guessed + "\n")
elif type(x) is not str or len(x) > 1:
print("Invalid guess, Your guess must be 1 letter long")
else:
print("\nWrong!")
guesses_left -= 1
show_guesses_left()
def start_game():
global letters_guessed
global word
global guesses_left
letters_guessed = ''.join(hidden_word)
while True:
if guesses_left > 0 and letters_guessed != word:
request_guess()
elif letters_guessed == word:
print("YOU WIN!!!, the word was " + word)
break
else:
print("\nYou lose! Better luck next time!")
break
start_game()
Notes
Got it to work!
Sorry short on time will be back to help more, but take a look around. I used some different methods than you originally had, seems like there was a issue with your letters_guessed not revealing letters past a letter already guessed. This will work for double letters as well, which also seemed to be an issued with your original code.
Again sorry, will be back to explain more!
The main problem is the code you use to get the index of the guessed letter:
t = word.find(x)
word = word.replace(x, "")
This shortens word after each correct guess, so t will not be the desired value after the first correct guess.
However, even if you fix this, you will still not properly handle the case that the guessed letter occurs multiple times.
Here is a short example that shows how to solve both problems:
answer = 'long'
hidden = '?' * len(answer)
print("Welcome to hangman!")
while True:
guess = input("Guess a letter: ")
result = ''
if guess in answer:
for answer_letter, hidden_letter in zip(answer, hidden):
if guess == answer_letter:
result += guess
else:
result += hidden_letter
hidden = result
print(hidden)
if hidden == answer:
print("You guessed it!")
break
The main problem is that you are basing that splicing on a variable that you modify on the go. In particular, the variable word, where you look for the position of x, the guessed letter. The position is correct until word gets modified in length, as you replace the letter with an empty string.
An easy fix for that is to simply change the replace statement, and put either an empty space, or another character, that the user would not normally put. In my example, I would replace:
word = word.replace(x, "")
with
word = word.replace(x, " ")
That of course breaks the program exit logic: you can never win.
There is still another issue, that is multiple occurrences of the same letter are not accounted properly. In fact, the program loops until exhaustion, when multiple instances of the same letter are in word.
That is due to the fact that find will only reveal the position of the first instance of a given letter, and you don't account for possible duplicates.
There are several ways to fix that, but I think the main issue is identified.
For an alternate implementation, check https://eval.in/1051220
I am making a hang man game. I am trying to cycle through a word and have all the repeats in the letter be appended to my list. For example the word "hello": if the user types in "l" I want all the l's to be added to my list. Right now it is only finding one "l" and if the user types an "l" again it finds the second "l".
I also want the user not to be able to type in another letter if they previously already typed it in.
I have two lists one for right guesses and wrong guesses that store every guess. For example if a user types in "h" in "hello"
"h" is a right guess so it appends to [h] but if they type in "h" again it adds it to the list so it says ["h","h"]. The wrong box works the same way but for words that are wrong. If they type in "z" for the word "hello" it says ["z"] in the wrong box.
Here is my code:
import random
user_input = ""
turns = 5
print("Welcome to Advanced Hang Man!")
print("Use your brain to unscramble the word without seeing its order!")
words = ["hello","goolge","czar","gnat","relationship","victor","patric","gir","foo","cheese"]
# Picks a random word from the list and prints the length of it
random_word = (random.choice(words))
random_word_legnth = (len(random_word))
print("Hint! The length of the word is",random_word_legnth)
hold_random_word = [i for i in random_word]
while turns != 0 and set(right_guess) != set(hold_random_word):
user_input = input("Please type your guess one letter at a time:")
right_guess = []
wrong_guess = []
#Calculating every input
if len(user_input) == 1 and user_input.isalpha():
for i in user_input:
if i in hold_random_word:
right_guess.append(i)
else:
wrong_guess.append(i)
print("Correct guess", ''.join(right_guess))
print("Wrong guess", ''.join(wrong_guess))
I'm not sure what your direct question is, but thinking about a hangman game you want to take the users guess and parse the entire word or phrase they are guessing to see if their guess matches anywhere in the word. I made a hang man game below that will function as expected (minus any error handling) Let me know if any parts confuse you, and i can explain
import random
wordcomp = []
wordguess = []
#this is a word bank for all puzzles, they are randomly chosen
word_bank = ['guess this phrase', 'Lagrange', 'foo', 'another phrase to guess']
# This loop stores each letter in a list, and generates a blank list with correct spaces and blanks for user
rand_word = random.randrange(4)
for i in word_bank[rand_word]:
wordcomp.append(i)
if i == ' ':
wordguess.append(' ')
else:
wordguess.append('__')
print('I am thinking of a word,' , wordguess , ' it has ', len(wordguess), ' characters total, GOOD LUCK \n')
wordlist = wordcomp
count = 0
placeletter = 0
wrongguess = []
guesscount = 0
while wordlist != wordguess:
guess = input('please input a lower case letter within the english alphabet!') ##Check that input is one character, and lower case
guesscount = guesscount + 1
# This for loop scans through to see if the letter that was guessed is in the actual puzzle, and places in the correct spot!!
for t in wordcomp:
if t == guess:
wordguess[placeletter] = guess
placeletter = placeletter + 1
# This check tells them they already guessed that letter... then makes fun of them
if guess in wordguess:
pass
else:
wrongguess.append(guess)
while wrongguess.count(guess) > 1:
wrongguess.remove(guess)
print('you guessed the letter ' , guess , ' already, are you person that suffers short term memory loss...')
print('The word I am thinking of: ' , wordguess)
print('The letters you have already guess are: ', wrongguess)
placeletter = 0
# This tells them they finished the puzzle and the number of guesses it took, if its over 26, it calls them stupid for obvious reasons...
if guesscount >= 26:
print('you are an idiot if it took you more than 26 guesses..... now take a minute, sit silently, and think about why you are idiot if it took over 26 guesses... for hangman... where you guess the letters of the alphabet... YOU GET IT, stupid')
elif guesscount < 26:
print('Congrats you solved the puzzle, w00t!!')
if len(user_input) == 1 and user_input.isalpha():
for i in user_input:
if i in hold_random_word and i not in right_guess:
right_guess.append(i)
elif i not in hold_random_word or i not in wrong_guess:
wrong_guess.append(i)
elif i in hold_random_word:
# here user types something that is already typed and is a right_guess
pass
else:
# Types something wrong, that was already typed
pass
print("Correct guess", ''.join(right_guess))
print("Wrong guess", ''.join(wrong_guess))
It is not clear how you are taking inputs, but I think this code can be further optimized. Give it a shot.
Edit 1:
import random
user_input = ""
turns = 5
print("Welcome to Advanced Hang Man!")
print("Use your brain to unscramble the word without seeing its order!")
words = ["hello","goolge","czar","gnat","relationship","victor","patric","gir","foo","cheese"]
random_word = (random.choice(words))
random_word_legnth = (len(random_word))
print("Hint! The length of the word is",random_word_legnth)
hold_random_word = [i for i in random_word]
# This condition can lead to issues in situations like this - abc and aabbcc [sorry couldn't quickly come up with a good actual example :)]
while turns != 0 and set(right_guess) != set(hold_random_word):
user_input = input("Please type your guess one letter at a time:").strip()
right_guess = []
wrong_guess = []
#Calculating every input
if len(user_input) == 1 and user_input.isalpha():
# user_input is 1 letter so for i in user_input will execute only once
# Use the if structure as defined above
if user_input in hold_random_word:
right_guess.append(i)
else:
# this is missing
turns -= 1
wrong_guess.append(i)
print("Correct guess", ''.join(right_guess))
print("Wrong guess", ''.join(wrong_guess))
elif len(user_input) > 1:
print("Please type only one letter at a time")
elif not user_input.isalpha():
print("Please enter only valid English letters")
else:
# handle this however you want :)
pass