2

I'm trying to make a hangman game (with only 6 letter words) and I'm trying to write the code for when there is more than 1 of a certain letter (inputted by the user) in the word

    tries = 0
    n = 0
    word = random.choice(word_list)
    print(word)
    while  tries<10:
        guess = input("Input a letter: ")
        if guess in word:
            n = n + 1
            print("Correct. You've got,", n,"out of 6 letters.")
            if n == 6:
                print("You guessed correctly, the word was,", word)
                break
        elif word.guess(2):
            n = n + 2
            print("Correct. You've got,", n,"out of 6 letters.")
            if n == 6:
                print("You guessed correctly, the word was,", word)
                break

although the program continues after a double letter is inputted for guess (eg. 's' in 'across') it still doesn't add up the correct number in the 'n' variable

STRhythm
  • 113
  • 8
  • It may help: https://trinket.io/python3/5e26836adf https://stackoverflow.com/a/55494835/797495 – Pedro Lobito Nov 14 '19 at 11:37
  • Does this answer your question? [Count the number occurrences of a character in a string](https://stackoverflow.com/questions/1155617/count-the-number-occurrences-of-a-character-in-a-string) – Noki Nov 14 '19 at 11:44
  • 1
    Also it seems that you're not checking if a letter has already be chosen by the user so you could actually win by spamming one correct letter. – Corentin Pane Nov 14 '19 at 11:49

4 Answers4

2

You can use count() for that. Use it to get the number of occurence in the word. This returns 0 if character is not present in word. Like inside your while after input() -

c = word.count(guess)
if c:
    n += c
    if n == 6:
        print("You guessed correctly, the word was,", word)
        break
    print("Correct. You've got,", n, " out of 6 letters.")

You may want to check whether user input is indeed a character and not a string or ''(empty string). That may confuse the program. Also you are not incrementing tries variable. So, user may get unlimited turns to try

Also, what is word.guess(2) in your code. Is that a typo?

kuro
  • 3,214
  • 3
  • 15
  • 31
1

You also need to remove letters that have been guessed already. You can do this using the replace function. Like so:

tries = 0
n = 0
word = random.choice(word_list)
word_updated = word
print(word)
while  tries<10:
    if n == 6:
        print("You guessed correctly, the word was,", word)
        break
    guess = input("Input a letter: ")
    if guess in word:
        n += word_updated.count(guess)
        word_updated = word_updated.replace(guess, "")
        print("Correct. You've got,", n,"out of 6 letters.")
Daniel Wyatt
  • 960
  • 1
  • 10
  • 29
0

i took the liberty to implement this

import random          

from collections import Counter

attempt = 0
tries = 10
correct = 0
character_to_check = 6
word_list = ['hello','hiedaw','rusiaa','canada']
word = list(random.choice(word_list))
print(word)
dic = dict(Counter(word))
while attempt <= tries:
    if correct==character_to_check :
        print("You guessed correctly, the word was {}".format(word))
        correct = 0 

        break

    guess = str(input("enter a letter "))
    if len(guess)>1 or len(guess)==0:
        print("only 1 letter")
    else:
        if guess in word:
           if dic[guess]>0: 
               correct += 1
               dic[guess]-=1
               print("you have guessed correct, you got {} out of {} letter".format(correct, character_to_check))
           else:
               print("already guessed this character")
    attempt+=1
    if attempt>tries:
        print("attempt exceed allowed limit")

output

['r', 'u', 's', 'i', 'a', 'a']

enter a letter 'r'
you have guessed correct, you got 1 out of 6 letter

enter a letter 'r'
already guessed this character

enter a letter 'u'
you have guessed correct, you got 2 out of 6 letter

enter a letter 's'
you have guessed correct, you got 3 out of 6 letter

enter a letter 'i'
you have guessed correct, you got 4 out of 6 letter

enter a letter 'a'
you have guessed correct, you got 5 out of 6 letter

enter a letter 'a'
you have guessed correct, you got 6 out of 6 letter
You guessed correctly, the word was ['r', 'u', 's', 'i', 'a', 'a']
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

A funny and unreadable one-liner could do the whole trick:

print(globals().update({"word": __import__("random").choice(['collection','containing','random','words']), "checked" : set(), "f" : (lambda :  f.__dict__.update({"letter" : input("Input a letter:\n")}) or ((checked.add(f.letter) or (print("Good guess!") if f.letter in word else print("Bad guess..."))) if f.letter not in checked else print("Already checked...")) or (print("You guessed correctly the word " + word + "!") if all(l in checked for l in word) else (print("Too many attempts... You failed!") if len(checked) > 10 else f())))}) or f() or "Play again later!")

(Do not use in production code probably)

I am using a set to remember both attempted letters and number of attemps, no need for another variable.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29