mercredi 7 juillet 2021

Cannot print output when variable meet the desired condition

This is a simple HANGMAN game, when you input a character it checks whether the character is true or false, if it's true you can continue without any penalty if it's not, you can proceed with -1 health penalty until you finish the game.

The problem is, when it meets 0 health, it cannot print a message that I wrote as it should be.

import random
from words import words_list

def play_game():

    random_num = random.randint(0, (len(words_list) - 1))
    current_word = list(words_list[random_num].upper())
    tries = 0
    lifes = 8
    tebakan_salah = 0
    first_input = ""
    picked_letters = []
    correct_ones = []
    
    print()
    print('MEMILIKI ', str(len(current_word)), ' HURUF')
    print('TEBAKAN SALAH : ',str(tebakan_salah))
    print('SISA TEBAKAN ANDA :',str(lifes))
    print()

    for i in range(len(current_word)):
        correct_ones.append('_ ')

    while tries == 0 :
        for i in range(len(current_word)):
            print('_ ', end='')
            tries += 1
            continue
        
        while lifes == 0 :
            lifes -=1
            return 'TEBAKAN ANDA GAGAL, SILAHKAN COBA LAGI'

        while lifes > 0:
            if lifes == 0 :
                lifes -=1
                print('TEBAKAN ANDA GAGAL, SILAHKAN COBA LAGI')
                break
            else:
            # Check if all characters have been already guessed
                if current_word == correct_ones:
                    print('\nSELAMAT ANDA BERHASIL!')
                    break

                current_letter = input('\nMASUKKAN TEBAKAN ANDA : ').upper()

                # Check if character has already been chosen
                if current_letter in picked_letters:
                    print('\nANDA TELAH MEMILIH HURUF INI!')
                    print()
                    continue

                # Check if character is in word
                if current_letter in current_word:
                    index_list = []
                    for i in range(len(current_word)):  # Get indexes of character in word
                        if current_word[i] == current_letter:
                            index_list.append(i)

                    picked_letters.append(current_letter)  # Append to keep track of chosen characters
                    for i in index_list:
                        correct_ones[i] = current_letter.upper()  # Append to correct position

                    print('BENAR!')
                    for i in correct_ones:
                        print(i + ' ', end='')

                # Incorrect character
                else:
                    picked_letters.append(current_letter)
                    lifes -= 1
                    tebakan_salah += 1
                    print()
                    print('SALAH!')
                    print('TEBAKAN SALAH : ',str(tebakan_salah))
                    print('SISA TEBAKAN ANDA : ',str(lifes))
                    print()
                    continue

play_game()

Aucun commentaire:

Enregistrer un commentaire