forked from zalakbhalani/PythonProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.py
More file actions
70 lines (62 loc) · 1.64 KB
/
Hangman.py
File metadata and controls
70 lines (62 loc) · 1.64 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
import random
import csv
def convert_lst_to_str(s):
movie = ""
return (movie.join(s))
def convert_str_to_list(l):
movie = []
return list(l)
name = input("What is your name? ")
print("Hello, " + name, "Time to play hangman!")
filename = "bollywood.csv"
rows = []
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
rows.append(row)
rows1 = []
for i in rows[1:]:
if int(i[0]) >= 2008:
rows1.append(i[1])
movie1 = random.choice(rows1)
p = "_" * len(movie1)
list1 = []
for i in range(len(movie1)):
if movie1[i] == " ":
list1.append(i)
movie2 = convert_str_to_list(p)
for j in list1:
movie2[j] = " "
movie3 = convert_lst_to_str(movie2)
print(movie3)
turns = 9
while turns:
a = str(input("Enter word for the film: "))
list2 = []
if (a in movie1) or (a.upper() in movie1):
for i in range(len(movie1)):
if (movie1[i] == a) or (movie1[i] == a.upper()):
list2.append(i)
movie2 = convert_str_to_list(movie3)
for j in list2:
if movie1[j].isupper():
movie2[j] = a.upper()
else:
movie2[j] = a
movie3 = convert_lst_to_str(movie2)
print(movie3,"\n")
list2.clear()
if movie3 == movie1:
print("You won!!")
break
else:
print("You guessed the wrong word")
turns -= 1
print("You have", + turns, 'more guesses')
print(movie3,"\n")
if turns == 0:
if movie3 == movie1:
print("You won!!")
else:
print("You lost.")
print("The title of movie is : " ,movie1)