forked from anshrathod/Basic-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
83 lines (63 loc) · 1.74 KB
/
Copy pathhangman.py
File metadata and controls
83 lines (63 loc) · 1.74 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
import random
import sys
listword=["hello","computer","python","java","html","world","apple","windows"]
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)