1
1
import random
2
+ from collections import Counter
2
3
3
4
fruits = '''apple banana mango strawberry
4
5
orange grape pineapple apricot lemon coconut watermelon
5
6
cherry papaya berry peach lychee muskmelon'''
6
7
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'''
9
9
10
- word = fruits .split (" " )
10
+ fruitList = fruits .split ()
11
+ animalList = animals .split ()
11
12
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 } )
14
15
16
+ wordList = fruitList + animalList
17
+ word = random .choice (wordList )
18
+ category = word_category [word ]
19
+ print (word )
15
20
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 ))
17
66
67
+ except KeyboardInterrupt :
68
+ print ()
69
+ print ("Bye! Try Again..." )
70
+ exit ()
0 commit comments