-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangmanV2.py
58 lines (49 loc) · 3.23 KB
/
hangmanV2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Project Hangman version 2
import random #importing random for choosing a random variable from the list
print("H A N G M A N Version 2")
character = "----------------------------------------" # for guesses
wordlist = {"ask":'enquire',"bad":'awful','deal':'bargain','act':'behave','brave':'courageous',
'amazing':'incredible','awful':'dreadful','big':'enormous','come':'approach',
'risky':'dangerous','do':'execute','fat':'stout','funny':'humorous'}
input_user = input('Type "play" to learn a new word, "exit" to quit: ')
while input_user.lower() == 'play':
random_variable = list(random.choice(list(wordlist.items()))) #choosing a random variable from wordlist this will come as tuple
random_variable2 = list(random_variable) #converting tuple to list
userGuesslist = list(character[0:len(random_variable2[1])]) #creating a list of letter with same number of '-'
listrandom = list(random_variable2[1]) #creating a list from random_variable selected
inputlist = [] #an empty list to take user input
def printGuessedLetter(): #function to make the word
return ''.join(userGuesslist)
incorrect = 0 #a variable to count the incorrect input given by user
while incorrect <= 8:
print()
print(printGuessedLetter(),random_variable2[0])
letter = input("Input a letter: ")
if len(letter) > 1: #checking for a single letter to be inputted
print("You should input a single letter")
continue
if ord(letter) < 97 or ord(letter) > 122: #checking letter should be from alphabet
print("It is not an ASCII lowercase letter")
continue
inputlist.append(letter) #appending the inputted letter to the inputlist
if letter.lower() in random_variable2[1]: #checking for the letter in random_variable
for i in range(len(random_variable2[1])):
if letter.lower() == listrandom[i]: #finding the position of letter from the listrandom
userGuesslist[i] = letter.lower() #replacing '-' with the guessed letter
if inputlist.count(letter) > 1: #checking for if the inputted letter is inputted first time
print("You already typed this letter")
joinedList = ''.join(userGuesslist)
if joinedList.lower() == random_variable2[1].lower(): #comparing the word formed with the random_variable
print("\nYou guessed the word {}!".format(random_variable2[1]))
print("You survived!\n")
break
else: #if inputted letter not in the random_variable
if inputlist.count(letter) == 1: #checking if the letter inputted is the first time
print("No such letter in the word")
incorrect += 1
else: #incorrect letter inputted more than first time
print("You already typed this letter")
if incorrect == 8: #to stop the game if incorrect letter equals to 8
print("\nYou are hanged!\nThe unguessed word is {}!\n".format(random_variable2[1]))
break
input_user = input('Type "play" to learn a new word, "exit" to quit: ')