vendredi 15 mai 2020

isalpha (): return to false statement if have space

1.im doing a hangman game but it seem i have an error because my db have a space in the list where do i put the 'if' statemnent in the isalpha? 2. do i write the condition on the main of the function

    while not guessed and tries > 0:
    guess = input ("Please guess a letter or word :").upper()
    if len (guess) == 1 and guess.isalpha():  'containing letters in the alphabet'
        if guess in guessed_letters:  
            print("You already guessed the letter",guess)

        elif guess not in word:
            print(guess,"is not in the word")
            tries -= 1 'incorrect guess'
            guessed_letters.append(guess)
            score -= 1

        else:
            print("Well Done !",guess,"is in the word")
            guessed_letters.append(guess)
            word_as_list = list(word_completion)
            indices = [i for i,letter in enumerate (word) if letter == guess] #to get index i and enumerate letter of the index in each words
            score += 1

            for index in indices:  'to replace each index with guess'
                word_as_list[index] = guess
            word_completion = "".join (word_as_list) 'update new changes on word as list and convert' to string
            if "_" not in word_completion:
                guessed = True

    elif len (guess) == len(word) and guess.isalpha():
        if guess in guessed_words:
            print ("You already guessed the word", guess)

        elif guess != word :
            print (guess,"is not in the word")
            tries -= 1
            guessed_words.append(guess)
            score -= 1

        else:
            guessed = True
            word_completion = word
            score += 1

Aucun commentaire:

Enregistrer un commentaire