-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHangman.py
37 lines (33 loc) · 807 Bytes
/
Hangman.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
"""This uses a randomly selected word or a user input word and plays hangman with it."""
from random import randint
def tryLetter(letter, word, arr):
for x in range(len(word)):
if(letter==word[x]):
arr[x]=letter
return arr
def containt(letter, word):
for x in word:
if(x==letter):
return True
return False
wordBank=["test1","test2","test3"]
if(input('Do you want to use your own word? Y/N\n')=="Y"):
wrd=input('Enter your word\n')
else:
wrd=(wordBank[randint(0,len(wordBank)-1)])
arr=["_"]*(len(wrd))
tries=5
while(tries!=0):
choice=input("Enter a letter\n")
tries-=1
arr=tryLetter(choice,wrd,arr)
for x in arr:
if x==choice:
tries+=1
break
print(arr," ",tries," try/tries left.")
if(containt("_",arr)==False):
print("You win!")
break
if(tries==0):
print("You lose!")