|
| 1 | +import random |
| 2 | +name = input("Enter your name ") |
| 3 | +print("Welcome ",name) |
| 4 | +again = "yes" |
| 5 | +#words in each category |
| 6 | +fruits =["apple","pineapple","orange","mango","banana","cherry"] |
| 7 | +countries =["india", "australia","england","germany","austria","indonesia","japan","singapore"] |
| 8 | +superheroes = ["batman","batwoman","catwoman","hawkeye","supergirl","thor","flash"] |
| 9 | +vegetables = ["tomato","potato","brinjal","onion","mushroom"] |
| 10 | +#various stages |
| 11 | +stages = [ |
| 12 | +""" |
| 13 | +\t-------- |
| 14 | +\t| |
| 15 | +\t| |
| 16 | +\t| |
| 17 | +\t|""", |
| 18 | +""" |
| 19 | +\t-------- |
| 20 | +\t| | |
| 21 | +\t| |
| 22 | +\t| |
| 23 | +\t|""", |
| 24 | +""" |
| 25 | +\t-------- |
| 26 | +\t| | |
| 27 | +\t| O |
| 28 | +\t| |
| 29 | +\t|""", |
| 30 | +""" |
| 31 | +\t-------- |
| 32 | +\t| | |
| 33 | +\t| O |
| 34 | +\t| / |
| 35 | +\t|""", |
| 36 | + |
| 37 | +""" |
| 38 | +\t-------- |
| 39 | +\t| | |
| 40 | +\t| O |
| 41 | +\t| /| |
| 42 | +\t|""", |
| 43 | + |
| 44 | +""" |
| 45 | +\t-------- |
| 46 | +\t| | |
| 47 | +\t| O |
| 48 | +\t| /|\\ |
| 49 | +\t|""", |
| 50 | + |
| 51 | +""" |
| 52 | +\t-------- |
| 53 | +\t| | |
| 54 | +\t| O |
| 55 | +\t| /|\\ |
| 56 | +\t| /""", |
| 57 | + |
| 58 | +""" |
| 59 | +\t-------- |
| 60 | +\t| | |
| 61 | +\t| O |
| 62 | +\t| /|\\ |
| 63 | +\t| / \\ """, |
| 64 | + |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +while(again == "yes"): |
| 69 | + z=-1 |
| 70 | + print("Lets play hangman ") |
| 71 | + #choosing a category |
| 72 | + print("\n\t\tCATEGORIES\n1.Fruits\n2.Countries\n3.Superheroes\n4.vegetables\n") |
| 73 | + n = int(input("Enter the category u want ")) |
| 74 | + #chosing a rondom word from the choosen category |
| 75 | + if(n ==1): |
| 76 | + word = random.choice(fruits) |
| 77 | + elif(n ==2): |
| 78 | + word = random.choice(countries) |
| 79 | + elif(n==3): |
| 80 | + word = random.choice(superheroes) |
| 81 | + elif(n==4): |
| 82 | + word = random.choice(vegetables) |
| 83 | + #printing the word with blank spaces so that user can know the number of letters |
| 84 | + for i in word: |
| 85 | + print("_",end=" ") |
| 86 | + # having guess for storing all the right guesses alone and all_guess for storing all guesses from user |
| 87 | + guess =" " |
| 88 | + all_guess =" " |
| 89 | + #getting a letter from user |
| 90 | + ch = input("\n\nEnter the guessed character "); |
| 91 | +#int n = len(word) |
| 92 | + chance = 8 |
| 93 | + while (chance>0): |
| 94 | + #wrong is a varibale used to check if word is fully found or not |
| 95 | + wrong = 0 |
| 96 | + #printing the word with all right guesses made by the user |
| 97 | + for i in word: |
| 98 | + if (i == ch or i in guess): |
| 99 | + print(i,end=" ") |
| 100 | + else: |
| 101 | + print("_",end=" ") |
| 102 | + wrong+=1 |
| 103 | + #checking if the word is fully found |
| 104 | + if( wrong == 0): |
| 105 | + print("\n\nCONGRATULATIONS!! YOU WIN !!! YOU FOUND THE WORD") |
| 106 | + break |
| 107 | + #checking if the letter is in the word and user has guessed that letter for the 1st time |
| 108 | + if(ch in word and ch not in guess): |
| 109 | + print("\nGood attempt ",ch," is in the word ") |
| 110 | + guess +=ch |
| 111 | + #showing the hangamn image if the user had made even 1 wrong guess before |
| 112 | + if(z>=0): |
| 113 | + print (stages[z]) |
| 114 | + #checking if the letter is already guessed |
| 115 | + elif (ch in all_guess): |
| 116 | + print("\nYou have already guessed ",ch ) |
| 117 | + #checking if it is a wrong guess and incrementing the hangman image to next stage and decrementing the chances |
| 118 | + elif (ch not in word): |
| 119 | + print("\n",ch," not in the given word try again ") |
| 120 | + z+=1 |
| 121 | + print(stages[z]) |
| 122 | + chance-=1 |
| 123 | + print("\nYou still have ",chance," chances ") |
| 124 | + #loosing condition |
| 125 | + if( chance <1): |
| 126 | + print("\n\nSorry. YOU LOOSE!!!") |
| 127 | + print("The Word is ", word) |
| 128 | + break |
| 129 | + all_guess +=ch |
| 130 | + #getting the guessed cahr from user |
| 131 | + ch = input("\n\nEnter the guessed character "); |
| 132 | + #checking if user wants to play again |
| 133 | + again = input("Do you want to play again ") |
| 134 | + |
| 135 | + |
| 136 | + |
0 commit comments