lundi 3 février 2020

python 3.8. hangman game. if statements not being followed [duplicate]

So it's my first week of learning to code and I wrote a hangman game, everything works as expected, except I wanted to add in a difficulty setting using an imput and if statements.

The problem is, is that no matter what the input is, the difficulty setting is always 'easy' meaning the program ignored the first if statement.

both word_lists are in a seperate file and are accessed by the program yet only 'if difficulty == 'easy'' is ever followed.

sorry if this is hard to read, writing isn't my strong point. below is the problematic code.

thanks in advance.

        import random
from collections import Counter
from word_list import hard_word_list
from word_list import easy_word_list

def restart():
    start = input('Do you want to play hangman? ')
    if start == 'yes' or 'y':
        difficulty = input('do you want easy mode or hard mode? ')
        if difficulty == 'easy' or 'e':

            word = (random.choice(easy_word_list))
            turns = len(word) + 4
            guesses = ''
            correct = 0
            flag = 0
            print('Let\'s play hangman, guess the letters to reveal the word, but be careful not to run out of guesses!')
            for i in word:
                print('_', end=' ')
            print()
            while (turns != 0) and flag == 0:
                try:
                    guess = str(input('\nEnter a letter to guess: '))
                    turns -= 1
                except:
                    print('Try letters only')
                    continue
                if not guess.isalpha():
                    print('Enter letters only, it\'s not #twitter')
                    continue
                elif len(guess) > 1:
                    print('Enter only a single letter')
                    continue
                elif guess in guesses:
                    print('You have already guessed that letter')
                    continue
                if guess in word:
                    k = word.count(guess)
                    for _ in range(k):
                        guesses += guess
                for letter in word:
                    if letter in guesses and (Counter(guesses) != Counter(word)):
                        print(letter, end=' ')
                        correct += 1
                    elif Counter(guesses) == Counter(word):
                        print("you won")
                        flag = 1
                        break
                    else:
                        print('_', end=' ')
            print('')
            print("the word was {0}\n".format(word))
            restart()
        elif difficulty == 'h' or 'hard':

            word = (random.choice(hard_word_list))
            turns = len(word) + 2
            guesses = ''
            correct = 0
            flag = 0
            print("Your're a veteran I see.")
            print('Let\'s play hangman, guess the letters to reveal the word, but be careful not to run out of guesses!')
            for i in word:
                print('_', end=' ')
            print()
            while (turns != 0) and flag == 0:
                try:
                    guess = str(input('\nEnter a letter to guess: '))
                except:
                    print('Try letters only')
                    continue
                if not guess.isalpha():
                    print('Enter letters only, it\'s not #twitter')
                    continue
                elif len(guess) > 1:
                    print('Enter only a single letter')
                    continue
                elif guess in guesses:
                    print('You have already guessed that letter')
                    continue
                if guess not in word:
                    turns -= 1
                if guess in word:
                    k = word.count(guess)
                    for _ in range(k):
                        guesses += guess
                for letter in word:
                    if letter in guesses and (Counter(guesses) != Counter(word)):
                        print(letter, end=' ')
                        correct += 1
                    elif Counter(guesses) == Counter(word):
                        print("you won")
                        flag = 1
                        break
                    else:
                        print('_', end=' ')
            print('')
            print("the word was {0}\n".format(word))
            restart()
    else:
        print('okay, bye!')


restart()```

Aucun commentaire:

Enregistrer un commentaire