-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpiccato.py
89 lines (75 loc) · 2.51 KB
/
Impiccato.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
import csv
import random
player_guess = ""
letter = ""
try_list = []
# Function that reads the csv file and returns a list of films
def read_csv():
filtered = []
with open('./Dataset/netflix_titles.csv', encoding="utf8") as f:
reader = csv.DictReader(f)
for row in reader:
if row["country"] == "Italy":
filtered.append(row["title"])
return filtered
def try_list_guess(tentative):
try_list.append(tentative)
# Function that censors the word
def censure_word(word):
censored = []
for letter in word:
if letter == " ":
censored.append(" ")
else:
censored.append("_")
return censored
# The function that chooses a random word from the list
def random_word(films):
word = random.choice(films)
return word.lower()
# The function that checks if the player has guessed the word
def word_guess(guess):
if guess == word.lower():
return True
else:
return False
def letter_guess(guess):
while True:
if len(guess) > 1:
print("Inserisci una sola lettera!")
else:
for i in range(len(word)):
if guess == word[i]:
censored[i] = guess
return censored
break
def game():
attemmp = 5
print("_______________________________________________________")
print("Benvenuto nel gioco dell'impiccato!")
print("Il gioco consiste nel trovare la parola segreta, lettera per lettera.")
print("Se sbagli", attemmp, "tentativi, il gioco finisce e il tuo amico verrà impiccato!")
print("_______________________________________________________")
while True:
if attemmp > 0:
print("La parola da indovinare è: ", *censored)
print("--Hai ancora", attemmp, "tentativi --", "lettere già estratte" , try_list)
print("--Prova ad indovinare la parola intera--")
player_guess = input()
if word_guess(player_guess):
print("--BRAVISSIMO, HAI INDOVINATO!--")
break
else:
print("--Hai sbagliato!", "Prova ad indovinare una lettera--")
letter = input()
letter_guess(letter)
try_list_guess(letter)
attemmp -= 1
elif attemmp == 0:
print("Hai finito i tentativi, il tuo amico verrà impiccato!")
break
Films = read_csv()
word = random_word(Films)
print(word)
censored = censure_word(word)
game()