basic python hangman game

Solutions on MaxInterview for basic python hangman game by the best coders in the world

showing results for - "basic python hangman game"
Alexa
06 Apr 2018
1import random, time
2fruits = ['pear', 'mango', 'apple', 'banana', 'apricot', 'pineapple','cantaloupe', 'grapefruit','jackfruit','papaya']
3superHeroes = ['hawkeye', 'robin', 'Galactus', 'thor', 'mystique', 'superman', 'deadpool', 'vision', 'sandman', 'aquaman']
4userGuesslist = []
5userGuesses = []
6playGame = True
7category = ""
8continueGame = "Y"
9
10name = input("Enter your name")
11print("Hello", name.capitalize(), "let's start playing Hangman!")
12time.sleep(1)
13print("The objective of the game is to guess the secret word chosen by the computer.")
14time.sleep(1)
15print("You can guess only one letter at a time. Don't forget to press 'enter key' after each guess.")
16time.sleep(2)
17print("Let the fun begin!")
18time.sleep(1)
19
20while True:
21    #Choosing the Secret word
22    while True:
23        if category.upper() == 'S':
24            secretWord = random.choice(superHeroes)
25            break
26        elif category.upper() == 'F':
27            secretWord = random.choice(fruits)
28            break
29        else:
30            category = input("Please select a valid categary: F for Fruits / S for Super-Heroes; X to exit")
31
32        if category.upper() == 'X':
33            print("Bye. See you next time!")
34            playGame = False
35            break
36
37    if playGame:
38        secretWordList = list(secretWord)
39        attempts = (len(secretWord) + 2)
40
41        #Utility function to print User Guess List
42        def printGuessedLetter():
43            print("Your Secret word is: " + ''.join(userGuesslist))
44
45
46        #Adding blank lines to userGuesslist to create the blank secret word
47        for n in secretWordList:
48            userGuesslist.append('_')
49        printGuessedLetter()
50
51        print("The number of allowed guesses for this word is:", attempts)
52
53
54        #starting the game
55        while True:
56
57            print("Guess a letter:")
58            letter = input()
59
60            if letter in userGuesses:
61                print("You already guessed this letter, try something else.")
62
63            else:
64                attempts -= 1
65                userGuesses.append(letter)
66                if letter in secretWordList:
67                    print("Nice guess!")
68                    if attempts > 0:
69                        print("You have ", attempts, 'guess left!')
70                    for i in range(len(secretWordList)):
71                        if letter == secretWordList[i]:
72                            letterIndex = i
73                            userGuesslist[letterIndex] = letter.upper()
74                    printGuessedLetter()
75
76                else:
77                    print("Oops! Try again.")
78                    if attempts > 0:
79                        print("You have ", attempts, 'guess left!')
80                    printGuessedLetter()
81
82
83            #Win/loss logic for the game
84            joinedList = ''.join(userGuesslist)
85            if joinedList.upper() == secretWord.upper():
86                print("Yay! you won.")
87                break
88            elif attempts == 0:
89                print("Too many Guesses!, Sorry better luck next time.")
90                print("The secret word was: "+ secretWord.upper())
91                break
92
93        #Play again logic for the game
94        continueGame = input("Do you want to play again? Y to continue, any other key to quit")
95        if continueGame.upper() == 'Y':
96            category = input("Please select a valid categary: F for Fruits / S for Super-Heroes")
97            userGuesslist = []
98            userGuesses = []
99            playGame = True
100        else:
101            print("Thank You for playing. See you next time!")
102            break
103    else:
104        break
105