Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Set
- import random
- MAX_GUESS = 6
- HARD_LEVEL = {'easy': 50, 'middle': 70, 'advanced': 100}
- RAND_HINT = True
- DIC_COW = {
- 6: r"""
- ^__^
- (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
- """,
- 5: r"""
- (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
- """,
- 4: r"""
- \_______
- \ )\/\
- ||----w |
- || ||
- """,
- 3: r"""
- \_______
- \ )\/\
- ----w |
- ||
- """,
- 2: r"""
- \_______
- \ )\/\
- ----w
- """,
- 1: r"""
- \_______
- \ )
- ----w
- """,
- 0: r"""
- """
- }
- def readDic() -> str:
- pass
- def guessDic(word: str) -> None:
- if word is None or len(word) == 0:
- return
- global MAX_GUESS
- guess_set = set()
- word_set = set(word)
- guess_count = 0
- if RAND_HINT:
- rand_pick = random.choices(list(word_set), k = random.randrange(HARD_LEVEL['easy'] * len(word_set) // 100, len(word_set)))
- for c in rand_pick:
- guess_set.add(c)
- print('Welcome to Hangman!')
- printLeft(MAX_GUESS - guess_count)
- printDic(word, guess_set)
- while guess_count < MAX_GUESS:
- while True:
- s = input('Guess your letter: ')
- if s.isalpha() and len(s) == 1:
- break
- if s in guess_set:
- print('Letter {} has been guessed!'.format(s))
- continue
- elif s not in guess_set and s in word_set:
- guess_set.add(s)
- guess_count += 1
- elif s not in guess_set and s not in word_set:
- guess_set.add(s)
- guess_count += 1
- print('Incorrect!')
- printDic(word, guess_set)
- if word_set.issubset(guess_set):
- print('Congratulation, you have make it after {} times try!'.format(guess_count))
- break
- printLeft(MAX_GUESS - guess_count)
- def printDic(word: str, guess_set: Set) -> None:
- l = []
- for c in word:
- if c not in guess_set:
- l.append('_')
- else:
- l.append(c)
- print(' '.join(l))
- def printLeft(guess_count: int) -> None:
- print(DIC_COW[guess_count])
- print('You have {} incorrect guesses left!'.format(guess_count))
- def main():
- words = ['hello', 'will', 'hiccup', 'fun', 'tiny', 'shock', 'water' \
- 'drink', 'quick', 'hopping']
- while True:
- guessDic(random.choice(words))
- i = ''
- while True:
- i = str(input('Do you want to play again (Y/y/N/n)? '))
- if i in ['Y', 'y', 'N', 'n']:
- break
- if i in ['Y', 'y']:
- continue
- elif i in ['N', 'n']:
- break
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement