-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman02.py
111 lines (104 loc) · 4.15 KB
/
hangman02.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Hangman 0.2
# Jason Culligan
# This actually works in 2023 in Python2, so do not update it to python3 in this file as it's the only working copy right now.
import random
import os
win = 1
gameswon = 0
runningtotal = 0
totgames = 0
word = ""
gallows = [
" _______\n|/ |\n| _O_\n| / | \\\n| |\n| / \\\n| / \\\n|________",
" _______\n|/ |\n| _O_\n| / | \\\n| |\n| / \\\n|\n|________",
" _______\n|/ |\n| _O_\n| / | \\\n| |\n|\n|\n|________",
" _______\n|/ |\n| _O_\n| / | \\\n|\n|\n|\n|________",
" _______\n|/ |\n| _O_\n|\n|\n|\n|\n|________",
" _______\n|/ |\n| O\n|\n|\n|\n|\n|________",
" _______\n|/ |\n|\n|\n|\n|\n|\n|________",
" _______\n|/\n|\n|\n|\n|\n|\n|________"
]
while win == 1:
win = 0
used = ""
tries = 7
word = (random.choice(open('./wordlist.txt').readlines())).rstrip()
os.system('clear')
print "I've selected a word. See if you can guess it."
# Print out underscores for each letter in the chosen word
print "\n", gallows[tries], "\n", tries
for i in word:
print "_",
# Main loop
while tries != 0:
holder = ""
print "\n"
guess = raw_input("Pick a letter: ").lower()[:1]
if guess in used:
for i in word:
if i in used:
print i,
holder += i
else:
print "_",
holder += "_"
print "\n\n", "You've already used that letter. Try a different one."
print "\n\n", "You have", tries, "tries left"
print "Letters used so far:", used
else:
used += guess
os.system('clear')
print "I've selected a word. See if you can guess it.", word
print "\n", gallows[tries], "\n", tries
for i in word:
if i in used:
print i,
holder += i
else:
print "_",
holder += "_"
# WIN
if "_" not in holder:
print "\n\n""YOU WIN!!!! You scored",tries, "out of 10.",2*"\n"
gameswon += 1
runningtotal += tries
while True:
goagain = raw_input("Would you like another go? (yes or no) ").lower()
if goagain in ['yes', 'no', 'marmite']:
totgames += 1
break
else:
print "I don't understand that. Try 'yes' or 'no' instead."
if goagain == "yes":
win = 1
tries = 0
os.system('clear')
break
if goagain == "marmite":
print "Yep. Now you're fired. Thanks Marmite. >:("
exit()
else:
if gameswon == 1:
print "\nYou won 1 game. You got", runningtotal, "out of 10 which is", 100 * runningtotal / (totgames * 10),"%",2*"\n"
else:
print "You won", gameswon, "games. You got", runningtotal, "out of", (totgames * 10), "which is", 100 * runningtotal / (totgames * 10),"%",2*"\n"
exit()
if guess not in word:
tries -= 1
os.system('clear')
print "\n", gallows[tries], "\n", tries
print "\n\n", " You have", tries, "tries left"
print " Letters used so far:", used
#LOSE
if win == 0:
print "\n"
print "Hard luck. The word I was thinking of was '%s'" % word, 2*"\n"
totgames += 1
if gameswon == 1:
print "You won 1 game. You got", runningtotal, "out of", (totgames * 10), "which is", 100 * runningtotal / (totgames * 10),"%", 2*"\n"
exit()
if gameswon == 0:
print "You got zero. Better luck next time!",2 *"\n"
else:
print "You won", gameswon, "games. You got", runningtotal, "out of", (totgames * 10), "which is", 100 * runningtotal / (totgames * 10),"%", 2*"\n"
exit()