-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathhangman.py
More file actions
91 lines (70 loc) · 1.92 KB
/
hangman.py
File metadata and controls
91 lines (70 loc) · 1.92 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import random
import sys
import urllib.request
def get_wordlist():
word_url = "https://www.mit.edu/~ecprice/wordlist.10000"
response = urllib.request.urlopen(word_url)
long_txt = response.read().decode()
words_list = long_txt.splitlines()
return words_list
listword = get_wordlist()
guessword=[]
random_word=random.choice(listword)
lenghtword=len(random_word)
alphabet="abcdefghijklmnopqrstuvwxyz"
letter_storage=[]
def intro():
print("\tHello and Welcome to Hangman (A word prediction game)")
while True:
name=input("Enter your name:\n").strip()
if name=="":
print("Enter a valid name\n")
else:
break
print("\n\t\tSo %s welcome to the Game :) " % name)
intro()
def game():
while True:
String=input("So you ready to play :\n ")
if String=="yes" or String=="Y" or String=="y":
break
elif String=="No" or String=="N" or String=="n":
sys.exit()
else:
print("Please Enter something ")
continue
game()
def rules():
for character in random_word:
guessword.append("_")
print("Ok, so the word You need to guess has", lenghtword, "characters")
print("Be aware that You can enter only 1 letter from a-z\n\n")
print(guessword)
def guessing():
guess_no=1
while guess_no<10:
guess=input("\nPick a letter : ")
if not guess in alphabet:
print("pick a letter from a-z ")
elif guess in letter_storage:
print("Already guessed this letter.")
else:
letter_storage.append(guess)
if guess in random_word:
print("You guessed correctly")
for x in range(0,lenghtword):
if random_word[x]==guess:
guessword[x]=guess
print(guessword)
if not '_' in guessword:
print("You won")
break
else:
print("Guessed letter not in the word")
guess_no+=1
if guess_no==10:
print("Sorry, you have used all your chances. YOU LOST !!")
rules()
guessing()
print("\tGAME OVER !! ")
# By: Darsh Asawa (https://github.com/DarshAsawa)