user14587589
user14587589

Reputation: 459

String mutation Python

I am building hangman in python. The first two functions I have generates random word and its definition from the API which is working just perfectly.

import requests
import json

def generate_random_word():
    random_word_url = 'https://random-word-form.herokuapp.com/random/adjective'
    random_word_url_response = requests.get(random_word_url)
    return json.loads(random_word_url_response.text)[0]

def get_random_word_definition():
    random_word = generate_random_word()
    definition_api_url = 'https://api.dictionaryapi.dev/api/v2/entries/en/' + random_word
    definition_api_url_response = requests.get(definition_api_url)
    return json.loads(definition_api_url_response.text)[0]['meanings'][0]['definitions'][0]['definition'],random_word

My next function is this:

def modify_word(word):
    new_word = list(word)
    for i in range(len(new_word)):
        new_word[i] = '_'
    return "".join(new_word)  

As you see it iterates other listed word and changes its characters to "_".

Now to the issue:

def validate_word(original,guess,hidden_word):
    listify_original = list(original)
    listify_hidden = list(hidden_word)
    for char_place,char in enumerate(listify_original):
        if(char == guess):
           listify_hidden[char_place] = char    
           print('found it',"".join(listify_hidden))  

This function is accepting 3 parameters: 1.Original word which was generated 2.Players guess 3.Hidden word which is generated through modify_word function above

Whenever user gets correct guess the hidden latter in string is replaced with this letter as expected so if the word is "colour" and user types c the function will print out string like this c_____ as it should. But issue comes when user guesses another latter. in this case whole string is being re-generated and letter guessed before last one just disappears. so for example if user guessed "l" function will print out __l___ instead of c__l___.

This is the main function in which I call validate_word:

    def hang_man_guess_game():
    generate_word = get_random_word_definition()
    definition = generate_word[0]
    word = generate_word[1]
    amount_of_tries = 6
    print('Welcome to the hang man game!')
    print('The length of the word is: ',len(word))
    print('The defition of word is: ',definition)
    print('Amount of health: ',amount_of_tries)
    print('Health gets lesser on each wrong guess!')
    print('You can write as many latters as you want!')
    print('Good luck player!')
    hidden_word = modify_word(word)
    while(amount_of_tries != 0):
        print('Word: ', hidden_word)
        user_guess = input('Enter your guess: ')
        validate_word(word,user_guess,hidden_word)
            # print('Good job')

hang_man_guess_game()

Sample input for word is: hopeful

Any suggestions please?

Upvotes: 0

Views: 120

Answers (4)

Bendik Knapstad
Bendik Knapstad

Reputation: 1458

So you'll have to make the function return the updated hidden word:

def validate_word(original,guess,hidden_word):
     listify_original = list(original)
     listify_hidden = list(hidden_word)
     for char_place,char in enumerate(listify_original):
         if(char == guess):
            listify_hidden[char_place] = char
            print('found it',"".join(listify_hidden)
            )
            return "".join(listify_hidden)

And then in your while loop:

while(amount_of_tries != 0):
        print('Word: ', hidden_word)
        user_guess = input('Enter your guess: ')
        hidden_word = validate_word(word,user_guess,hidden_word) <-- update the hidden word
            # print('Good job')

Upvotes: 1

vnk
vnk

Reputation: 1082

You are not updating hidden_word as the user guesses a letter right. You can do this by returning the updated value in validate_word()

def validate_word(original,guess,hidden_word):
    listify_original = list(original)
    listify_hidden = list(hidden_word)
    current_word = hidden_word
    for char_place,char in enumerate(listify_original):
        if(char == guess):
           listify_hidden[char_place] = char    
           current_word = "".join(listify_hidden)
           print('Found it', current_word)  
           
    return current_word

Similarly in the while loop in hang_man_guess_game(), the returned value now becomes the updated hidden_word

while(amount_of_tries != 0):
    print('Word: ', hidden_word)
    user_guess = input('Enter your guess: ')
    hidden_word = validate_word(word,user_guess,hidden_word)

Output

Enter your guess: q
Word:  _____
Enter your guess: c
Found it c____
Word:  c____
Enter your guess: r
Found it c___r
Word:  c___r
Enter your guess: o
Found it co__r
Found it co_or
Word:  co_or
Enter your guess: l
Found it color
Word:  color

Upvotes: 1

Pantoofle
Pantoofle

Reputation: 356

In your validate_word function, the hidden_word variable is never modified. At the beginning, you create a new list, called listify_hidden, but when you change a letter in this variable, it does not propagate to the original hidden_word string, outside of the function.

One way to fix this would be to make your validate_word return the new hidden string, with the discovered letters.

Upvotes: 1

3dSpatialUser
3dSpatialUser

Reputation: 2406

You should update your hidden_word with based on the function validate_word. You should return "".join(listify_hidden). You can then move you print function in the main, this is in my opinion good practice to make sure each function only has one task (i.e. validate the word and printing is another task).

Upvotes: 1

Related Questions