So I have a quick question about the below code. In the program it will display a series of dashes to match the random word selected. When the player guesses, the program goes through a loop that checks the guess. My question is, and what I'm really confused over, is how does the program know to replace the exact dash needed to display an accurate and partially revealed word? and what is new += so_far[i] doing? overall, that section really confuses me and I would greatly appreciate some clarification. Thanks!
MAX_WRONG = len(HANGMAN) - 1
# creating list of random words
WORDS = ("TECHNOLOGY", "PYTHON", "SCRIPT", "PROCESSOR", "RANDOM", "COMPUTING")
# initialize variables
word = random.choice(WORDS)
so_far = "-" * len(word) # one dash for every letter in the word to be guessed
wrong = 0 # number of wrong guesses made
used = [] # letters already guessed
# creating the main loop
print("Welcome to Hangman. Good luck!")
while wrong < MAX_WRONG and so_far != word:
print(HANGMAN[wrong])
print("\nYou've used the following letters:\n", used)
print("\nSo far, the word is:\n", so_far)
# getting the player's guess
guess = input("\n\nEnter your guess: ")
guess = guess.upper()
while guess in used:
print("You've already guessed the letter", guess)
guess = input("\nEnter your guess: ")
guess = guess.upper()
used.append(guess)
# checking the guess
if guess in word:
print("\nYes!", guess, "is in the word!")
# create a new so_far to include guess
new = ""
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
else:
print("\nSorry,", guess, "isn't in the word.")
wrong += 1
# ending the game
if wrong == MAX_WRONG:
print(HANGMAN[wrong])
print("\nYou lose!")
else:
print("\nYou guessed it!")
print("The word was:", word)
ext()
You are looking out for this section in the code which does that
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
Say if you guess the word 'S' and say if the word to be guessed is 'Super'
Initial state
Sofar : -----
During the iteration process a variable 'new' is assigned the value for each word match. if not matched take that value from so_far[i]
In our case
Length of word = len('Super') = 5
Iter 1: will only go to ig loop since 'S' occurs only once.
This means all other iterations go to else condition and since all so_far[i] has '-' as values.
So after the first loop of iteration your values look like this
new: S----
At the end of loop reassign new to so_far. This means now 'so_far' variable has changed from '-----' to 'S----'.
I suggest you to use a pdb to debug above the for loop and get the details when you get to such doubts in future.
Hope this helps.
Related
I'm writing a word guessing game like, what I'm expecting is that results appear in uppercase when is on the right spot. Until now, I write the program and it completely works but, let me show you... This is how it goes:
print("Welcome to the word guessing game!")
print(" ")
secret_word = "heaven"
guess_count = 0
display = '_'*len(secret_word)
(len(secret_word)-1)
word = ["_"] * len(secret_word)
while True:
guess = input("What is your guess? ")
for i in range(len(secret_word)):
if guess == secret_word[i]:
word[i] = guess
if guess == secret_word:
print("")
print("You guessed it!")
break
else:
print("".join(word))
guess_count = guess_count+1
print(f"It took you {guess_count} guesses")
So, I'm not sure where to put the uppercase function.
Since the letters are not present at all in the secret word, it shows an "_".
The letters that are present in the secret word, but in a different spot should be shown as lowercase.
The Letters that are present in the secret word at that exact spot, shown in uppercase.
I tried to find the answer on YT and other sites without success. I just want to understand which functions using (.uppercase) I can use to make it.
Hope you can help me, guys. Thanks in advance!
I do not understand. In your code, it seems as though the guess is a single character, but you do not ask for the spot which is needed to determine if it must be uppercase or lowercase. I tried to answer your question based on wordle, so I guessed the player would have to enter a word.
You'd need something like:
for i in range(len(secret_word)):
if guess[i] = secret_word[i]:
word[i] = guess[i].upper()
elif guess[i] in secret_word:
word[i] = guess[i].lower
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 am trying to design a hungman game using simple while loop and if else statement.
Rules of the game:
1.Random word is selected from a list of words
2.User is informed when the word is selected and asked to provide his/ her first guess
3.If the user's guess is correct, the letter is console and inform the user how many letters left
The user will get only 5 lives to play the game.
1.import random
2.import string
3.def hungman():
4.words = ["dog", "monkey", "key", "money", "honey"]
5.used_letters = []
6.word = words[random.randrange(len(words))]
7.lives=5
8.while len(word) > 0 and lives > 0:
9.guess = input("Please guess the letter: ")
10.if 'guess' in word:
11.print(guess, " is a correct guess")
12.used_letters.appened(guess)
13.if used_letters.len == word.len:
14.print("Congratulation, You have guessed the correct letter: ",word)
15.else:
16.remaining_letters = word.len - used_letters.len
17.print("You have", remaining_letters, "left")
18.else:
19.lives = lives - 1
20.if lives == 0:
21.print("Sorry! you don't have more lives to continue the game")
22.print("Correct word is: ", word)
23.break
24.else:
25.print("You have", lives , " left")
26.hungman()
The program should ask for the user input which will store in guess variable. If the letter given by the user is one of the letters of word string then the code prompts that the given input is correct and append the letter in the used_letters list. Else it shows the length of the remaining letters for wrong user input. Additionally, if the user fails to guess the letter correctly he or she will lose 1 life as well.
However, as per my code after the while loop in line number 8 the control goes to line no. 18 although I provided correct letter as user input. Line 10 to 17 totally discarded. I could not find the reason of this nature.
Please help me in this regard.
Thank you
You have a couple of issues in your code.
The one you mentioned is because of quotation marks in line 10. Should be
if guess in word:
in line 12 you have a typo
used_letters.append(guess)
to get the length of a list you should use function len(), e.g.
if len(used_letters) == len(word)
And finally you have an issue with exiting the loop in case of the correct answer. You should use
while len(word) > len(used_letters) and lives > 0:
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
My code is:
import random
WORDS = ('python', 'football', 'facebook', 'photo') #list of words that will be riddled
word = random.choice(WORDS)
correct = word
jumble = ''
hint = 'hint'
score = 0
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):] #creating jumble of correct words
print('Welcome to the game "Anagrams"')
print('Here`s your anagram:', jumble) #Welcoming and giving a jumble to a player
guess = input('\nTry to guess the original word: ')
if guess == correct:
score += 5
print('You won! Congratulations!') #end of game in case of right answer
if guess == hint: #situation if player asks a hint
if correct == WORDS[0]:
print('snake')
elif correct == WORDS[1]:
print('sport game')
elif correct == WORDS[2]:
print('social network')
elif correct == WORDS[3]:
print('picture of something')
score += 1
while guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
guess = input('Try to guess the original word: ')
print('Thank you for participating in game.')
print('Your score is', score)
input('\nPress Enter to end')
When asking hint string :
'Sorry, you`re wrong :('
repeats.
It looks like:
Try to guess the original word: hint
sport game
Sorry, you`re wrong :(
How to make this string appear only in case of wrong guess?
change you last while to this:
while guess != correct and guess != '':
guess = input("Sorry, you`re wrong:( ")
In your code, when the player types hint the player gets a hint, but then the program tests the 'hint' string against the correct word. Of course, 'hint' isn't the correct answer, so your program tells them that it's wrong.
Just for fun, I've optimized your code a little, and improved the scoring logic. :)
Your letter-jumbling for loop is quite clever, but there's a more efficient way to do this, using the random.shuffle function. This function shuffles a list, in place. So we need to convert the chosen word into a list, shuffle it, and then join the list back into a string.
I've also replaced your hints logic. Rather than having to do a whole bunch of if tests to see which hint goes with the current word it's much simpler just to store each word and its associated hint as a tuple.
import random
#Words that will be riddled, and their hints
all_words = (
('python', 'snake'),
('football', 'sport game'),
('facebook', 'social network'),
('photo', 'picture of something'),
)
#Randomly choose a word
word, hint = random.choice(all_words)
#Jumble up the letters of word
jumble = list(word)
random.shuffle(jumble)
jumble = ''.join(jumble)
print('Welcome to the game "Anagrams"\n')
print('You may ask for a hint by typing hint at the prompt')
print('Wrong guesses cost 2 points, hints cost 1 point\n')
print("Here's your anagram:", jumble)
score = 0
while True:
guess = input('\nTry to guess the original word: ')
if guess == word:
score += 5
print('You won! Congratulations!')
break
if guess == 'hint':
#Deduct a point for asking for a hint
score -= 1
print(hint)
continue
#Deduct 2 points for a wrong word
score -= 2
print('Sorry, you`re wrong :(')
print('Thank you for participating in game.')
print('Your score is', score)
input('\nPress Enter to end')
Your special logic for a correct guess and for the special input "hint" is only run once on the very first guess. Your loop for incorrect values always runs after that. I think you want to move all the logic into the loop:
while True: # loop forever until a break statement is reached
guess = input('\nTry to guess the original word: ')
if guess == correct:
score += 5
print('You won! Congratulations!')
break # stop looping
if guess == hint: # special case, asking for a hint
if correct == WORDS[0]:
print('snake')
elif correct == WORDS[1]:
print('sport game')
elif correct == WORDS[2]:
print('social network')
elif correct == WORDS[3]:
print('picture of something')
score += 1
else: #situation if player is not correct, and not askng for a hint
print('Sorry, you`re wrong :(')
I've left out the situation where your code would exit the loop on an empty input. If you want that, you should add it explicitly as an extra case, with a break statement.
Lets try to fix some problems:
this
if guess == hint: #situation if player asks a hint
should probably be
elif guess == hint: #situation if player asks a hint
And also this seems wrong to me
while guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
guess = input('Try to guess the original word: ')
should be probably changed into that (indentation is important):
guess = input('Try to guess the original word: ')
if guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
I have not tried this corrections in a complete program.