1

I'm trying to a hangman game. I already achieved to set the basics of the game (list, interaction with user) but I don't know how to do the lines for the game, keep asking and printing what the user correct answers are and ilustrate the hangman. I used index in order to search the exact location of the letter in the word, but I dont know to print it, depending on the number and also I don't how to code that the program keeps track of the correct words.

I would be totally glad for your help. Also thanks for your patience. The first part of the code is well coded but stackoverflow doesn't display it right.

------------------------------------------------------------------------

import random
def hangman():
    words = ['house','car','women', 'university','mother', 'school', 'computer','pants']   #list of words
    computer  = words[random.randint(0,6)]  #computerchoice
    word = list(computer)  #Make a list with each letter of the word. 

    welcome = (input ('Welcome, type ok to continue: '))
    if welcome == 'ok':
        length = len(word)
        print(f'help? the word has {length} letters')
        option = (input('Start guessing, letter by letter'))
        count= 0   #count takes the count of how many tries. 
        chances = length+3   #You are able to make 3 mistakes. 
        while count<=chances:
        
            if option in word:    #if the choice is there
                place = word.index(option)  #search for the place. 
                print()   #dont know how to print it in 'that' place. 
                #place the correct letter over that line.
                print('_ '*length)  #Trying to do the  under  lines. 
                count+=1
    else:
        break 
#Dont know to ilustrate the hangman depending on the length of the word. 


hangman()
Community
  • 1
  • 1

1 Answers1

4

First let's analyse your code:

import random
def hangman():
    words = ['house','car','women', 'university','mother', 'school','computer','pants']   #list of words
    computer  = words[random.randint(0,6)]  #computerchoice
    word = list(computer)  #Make a list with each letter of the word. 

Everything is fine up to here , although str can be used the same way as a list , so no need to transform it.

    welcome = (input ('Welcome, type ok to continue: '))
    if welcome == 'ok':
        length = len(word)
        print(f'help? the word has {length} letters') 

Yes but those are not unique letters. You can use set() to have the number of unique letters.

        option = (input('Start guessing, letter by letter'))

If your input starts here, you will only ask once for a letter , you need to include this part in the while loop

        count= 0   #count takes the count of how many tries. 
        chances = length+3   #You are able to make 3 mistakes. 

This would then probably be changed to the length of the set.

        while count<=chances:

            if option in word:    #if the choice is there
                place = word.index(option)  #search for the place. 

This will only give you the index of the first occurence. We should keep in mind to use regex for this type of search : Find all occurrences of a substring in Python

                print()   #dont know how to print it in 'that' place. 

Let's remember to use the print formating f'stufff{value}stuffff'

                #place the correct letter over that line.

To do it , you need to create a str only with _and then fill it in with the indexes using list comprehension .

                print('_ '*length)  #Trying to do the  under  lines. 
                count+=1

Maybe we should handle what happens if option is not in words ?

    else:
        break 
    #Dont know to ilustrate the hangman depending on the length of the word. 

Also there is no need for break : count increments and therefore while will terminate. And if it was for the outer if/else , break doesn't work outside a loop.

hangman()

Question for OP:

What point would you like to sort out yourself ? What do you need help for next ?

Born Tbe Wasted
  • 610
  • 3
  • 13
  • This is how help should be offered to new learners, props to you for the very informative and helpful answer +1 – Nordle Apr 05 '19 at 13:55