Skip to content

Commit 9fc38d8

Browse files
committed
Adding Third Project
1 parent 5c6368b commit 9fc38d8

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

Project 3 - Hungman Game/main.py

+59-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
11
import random
2+
from collections import Counter
23

34
fruits = '''apple banana mango strawberry
45
orange grape pineapple apricot lemon coconut watermelon
56
cherry papaya berry peach lychee muskmelon'''
67

7-
animals = '''tiger lion elephant leopord
8-
gorilla hyena panther camel alligator panda mole'''
8+
animals = '''tiger lion elephant leopord gorilla hyena panther camel alligator panda mole'''
99

10-
word = fruits.split(" ")
10+
fruitList = fruits.split()
11+
animalList = animals.split()
1112

12-
word = random.choice(word)
13-
print(word)
13+
word_category = {word:'fruit' for word in fruitList}
14+
word_category.update({word:'animal' for word in animalList})
1415

16+
wordList = fruitList + animalList
17+
word = random.choice(wordList)
18+
category = word_category[word]
19+
print(word)
1520
if __name__ == '__main__' :
16-
print("Guess the Fruit Word!")
21+
print(f'Guess the Word! : Hint -> Category {category}')
22+
23+
rounds = len(word) + 2
24+
print(f'------> Total Number of Rounds {rounds} <------')
25+
letterGuessed = ''
26+
correct = 0
27+
flag = 0
28+
29+
try:
30+
for i in word:
31+
print("_", end=" ")
32+
print()
33+
while(rounds and flag == 0):
34+
guess = input("Enter a letter : ")
35+
if not guess.isalpha():
36+
print("Only Letters are Allowed !!!")
37+
continue
38+
if len(guess) != 1:
39+
print("Please Enter only Once Letter at a time")
40+
continue
41+
42+
if guess in word:
43+
c = word.count(guess)
44+
for _ in range(c):
45+
letterGuessed += guess
46+
47+
for char in word:
48+
if char in letterGuessed and (Counter(letterGuessed) != Counter(word)):
49+
print(char, end=" ")
50+
correct += 1
51+
elif (Counter(letterGuessed) == Counter(word)):
52+
print(f'The word is : ', end=" ")
53+
print(word)
54+
flag = 1
55+
print("Congratulations ***")
56+
break
57+
break
58+
else:
59+
print('_', end=" ")
60+
rounds -= 1
61+
62+
if rounds <= 0 and (Counter(letterGuessed) != Counter(word)):
63+
print()
64+
print("You Lost the Game !!!")
65+
print('The word is {}'.format(word))
1766

67+
except KeyboardInterrupt:
68+
print()
69+
print("Bye! Try Again...")
70+
exit()

0 commit comments

Comments
 (0)