python user picks a word and the computer guesses it

Okay. So, to start off as @Uvar pointed out, the assignment and comparison has been mixed up at several places. Additionally, the code could run for very long duration because of the while loop and indiscriminate picking of values between 0-25. The code doesn’t eliminate previously guessed digits in future guesses. The modified code (with comments) is as follows:

import random
import string

guessed_letter = 0

# list of all letters
# string.ascii returns 'abcdefghijklmnopqrstuvwxyz'
alphabet = list(string.ascii_lowercase)
alphabet.extend(list(string.ascii_uppercase))
generateletter = True


# this function is susposed to randomly picks letters and only move 
# on if that letter is not one in the guessed list
def new_letter(generateletter, alphabet):
    while generateletter:
        # Using random.choice to pick values
        guessed_letter = random.choice(alphabet)

        if guessed_letter not in guessed:
            for g in guessed:
                if guessed_letter == g:
                        continue
            generateletter = False
        guessed.append(guessed_letter)

        # To prevent picking the same values again, remove them
        alphabet.remove(guessed_letter)
        print('I guess '+str(guessed_letter))
        generateletter = False

    return guessed_letter

def display_status(actual_word, computers_word, guessed):
    print("Actual Word", actual_word)
    print("Computers Word:", computers_word)
    print("Guessed:", guessed)

playagain = True
guessagain = True

# 'while playagain == True' and 'while playagain' evaluate the same thing
while playagain:

    user_word = input('Please choose a word and I will try to guess it: ')

    guessed = []
    actual_word = []
    computers_word = []

    for letter in user_word:
        actual_word.append(letter)
        computers_word.append('*')

    while guessagain:
        if computers_word != actual_word:
            display_status(actual_word, computers_word, guessed)
            guessed_letter = new_letter(generateletter, alphabet)
            print("Guessed Letter:", guessed_letter)

            # Used enumerate here to get position too
            # Additionally, used actual_word
            for i,letter in enumerate(actual_word):
                if guessed_letter == letter:
                    # Insert at exactly the required position
                    computers_word[i] = guessed_letter

                else:
                    continue

        elif computers_word == actual_word:
            display_status
            user_choice = input('I guessed the word! Would you like to play again? ')
            if user_choice == 'Yes' or user_choice == 'yes':
                guessagain = False
                playagain = True

            else:
                guessagain = False
                playagain = False

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top