Browse Source

Prepare source for 2nd workshop

master
Dimitri Merejkowsky 5 years ago
parent
commit
e2d1cd9933
5 changed files with 97 additions and 52 deletions
  1. +0
    -52
      sources/04-pendu.py
  2. +79
    -0
      sources/hangman/hangman.py
  3. +16
    -0
      sources/hangman/scores.py
  4. +2
    -0
      sources/hangman/scores.txt
  5. +0
    -0
      sources/hangman/words.txt

+ 0
- 52
sources/04-pendu.py View File

@@ -1,52 +0,0 @@
import random


def read_words():
file = open("pendu.txt")
contents = file.read()
file.close()
words = contents.splitlines()
return words


def choose_word(words):
n = len(words)
index = random.randint(0, n-1)
return words[index]


def has_won(word, letters):
for letter in word:
if letter not in letters:
return False
return True


def display_hint(word, letters):
for letter in word:
if letter in letters:
print(letter, end="")
else:
print("_", end="")
print("")


def main():
words = read_words()
word = choose_word(words)
print(word)

letters = set()
display_hint(word, letters)

while True:
new_letter = input()
letters.add(new_letter)
display_hint(word, letters)
if has_won(word, letters):
print("Gagné")
return



main()

+ 79
- 0
sources/hangman/hangman.py View File

@@ -0,0 +1,79 @@
import sys
import random


def read_words():
file = open("words.txt")
contents = file.read()
file.close()
words = contents.splitlines()
return words


def choose_word(words):
n = len(words)
index = random.randint(0, n - 1)
return words[index]


def has_won(word, letters):
for letter in word:
if letter not in letters:
return False
return True


def display_hint(word, letters):
for letter in word:
if letter in letters:
print(letter, end="")
else:
print("_", end="")
print("")


def main():
num_tries = 10
words = read_words()
word = choose_word(words)

letters = set()
display_hint(word, letters)

while num_tries:
print(num_tries, "essai(s) restant(s)")

answer = input()
while answer in letters:
print("Lettre déjà proposée")
answer = input()

if len(answer) == len(word):
guess = answer
if guess == word:
print("Gagné")
sys.exit(0)
else:
print("Mauvaise réponse")
num_tries -= 1

elif len(answer) == 1:
letter = answer
letters.add(letter)
display_hint(word, letters)
if has_won(word, letters):
print("Gagné")
sys.exit(0)
else:
num_tries -= 1

else:
print("Veuillez entrer juste une lettre ou le mot entier")

print("Vous avez dépassé le nombre d'essais autorisés")
print("Le mot à deviner était :", word)
sys.exit(1)



main()

+ 16
- 0
sources/hangman/scores.py View File

@@ -0,0 +1,16 @@
def parse_scores():
res = dict()
with open("scores.txt") as file:
for line in file.readlines():
name, score = line.split()
res[name] = int(score)
return res


def main():
scores = parse_scores()
for name, score in scores.items():
print(name, score)


main()

+ 2
- 0
sources/hangman/scores.txt View File

@@ -0,0 +1,2 @@
alice 42
bob 33

sources/pendu.txt → sources/hangman/words.txt View File