I wanna count how many letters are in the word that i'm trying to guess and each time i guess a word correctly the variable correct_letters
will increase by 1
, but i don't even know why or how it sometimes increase by a lot of number and other times it doesn't even detect the condition that i put
enter image description here
beforehand thank you here is the code
def hangman():
myword = random_word () # this is just a random word that i chose from a list
print(myword)
used_letters= set()
letters_guessed= ""
#lookingfor = ""
count = 4
#loop til the user fail too many times
correct_letters= 0
while count > 0 or len(myword) > correct_letters:
lookingfor= input(" guess the letter")
letters_guessed = letters_guessed lookingfor # put a letter to guess in the word
used_letters =set(list(lookingfor))
print(len(myword)) #for know the number of letters that has the word in order to do an if statement with both at the end
print (correct_letters) #it is to measure how many letters we've guessed correctly
print(f"you used the letters: {used_letters}")
for letter in myword: # it evaluates each character in the variable "myword"
if letter in letters_guessed: # it says that if the current character is equal to the word guessed it prints all the letter in that word
print( f"{letter}", end="")
correct_letters = 1
else: #and else if the current character doesnt have the letter guessed it prints a "-" for example : "w-r-d"
print(f"-", end="")
if correct_letters == len(myword):
print(f"congratulations you guessed the word, it is {''.join(myword)}")
break
CodePudding user response:
Changes made:-
(1) In every iteration in while `correct_letters initialised to 0`
(2) If user input the letter which is not present in the word `count decreased by 1`
(3) format the answer in readable way..
(4) add the condition. if user can't predict the word. user displayed **Better luck next time your chance are over.. the correct word is:...**
Code:-
def hangman():
myword = "random" # this is just a random word that i chose from a list
print(myword)
used_letters= set()
letters_guessed= ""
#lookingfor = ""
count = 4
#loop til the user fail too many times
correct_letters= 0
while count > 0:
lookingfor = input("\nguess the letter: ")
if lookingfor not in myword:
count-=1
letters_guessed = letters_guessed lookingfor # put a letter to guess in the word
used_letters =set(list(lookingfor))
print(len(myword)) #for know the number of letters that has the word in order to do an if statement with both at the end
print(correct_letters) #it is to measure how many letters we've guessed correctly
print(f"you used the letters: {used_letters}")
correct_letters=0
for letter in myword: # it evaluates each character in the variable "myword"
if letter in letters_guessed: # it says that if the current character is equal to the word guessed it prints all the letter in that word
print( f"{letter} ", end="")
correct_letters =1
else: #and else if the current character doesnt have the letter guessed it prints a "-" for example : "w-r-d"
print(f"- ", end="")
if correct_letters == len(myword):
print(f"congratulations you guessed the word, it is {''.join(myword)}")
break
if not count:
print("\nBetter luck next time your chance are over.. the correct word is: " myword)
hangman()
Output:- When user guess correct:-
random
guess the letter: r
6
0
you used the letters: {'r'}
r - - - - -
guess the letter: a
6
1
you used the letters: {'a'}
r a - - - -
guess the letter: n
6
2
you used the letters: {'n'}
r a n - - -
guess the letter: d
6
3
you used the letters: {'d'}
r a n d - -
guess the letter: o
6
4
you used the letters: {'o'}
r a n d o -
guess the letter: m
6
5
you used the letters: {'m'}
r a n d o m congratulations you guessed the word, it is random
When user can't get it right
random
guess the letter: q
6
0
you used the letters: {'q'}
- - - - - -
guess the letter: z
6
0
you used the letters: {'z'}
- - - - - -
guess the letter: p
6
0
you used the letters: {'p'}
- - - - - -
guess the letter: t
6
0
you used the letters: {'t'}
- - - - - -
Better luck next time your chance are over.. the correct word is: random
CodePudding user response:
there you can see im trying to fulfill the condition
im trying to fulfill the condition but it doesnt follows the order of my condition it turns a little crazy, also the number that is above of the "0" the number "8" is the number of letters that the word has and the 0 it suppose that is the count of correct letters that i guessed but it doesnt work as it suppose to be