Skip to content

Instantly share code, notes, and snippets.

@yipyip
Last active December 19, 2015 23:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yipyip/6038584 to your computer and use it in GitHub Desktop.
Save yipyip/6038584 to your computer and use it in GitHub Desktop.
wrong naming in make_task: chars->markers
#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
"""A Simple Hangman Demo for Python3.
"""
####
import random as rand
####
def hangman(solution, percent=0, placeholder='_', prompt='? '):
"""Hangman game loop.
"""
task = make_task(solution, percent, placeholder)
while placeholder in task:
print(task)
guess = input(prompt)
if not guess:
continue
guess0 = guess[0]
task = ''.join(guess0 if guess0 == solution_char else task_char
for task_char, solution_char in zip(task, solution))
else:
print(task)
####
def make_task(solution, percent, placeholder):
"""Return list of chars to guess.
"""
task_len = len(solution)
show_len = int(task_len * percent / 100.0)
markers = [1] * show_len + [0] * (task_len - show_len)
rand.shuffle(markers)
return ''.join(char if i else placeholder for char, i in zip(solution, markers))
####
def get_words(placeholder):
"""Return list of words to guess.
"""
import builtins
return [s.lower() for s in builtins.__dict__ \
if not placeholder in s]
####
def main(conf):
"""Play multiple rounds of hangman.
"""
words = get_words(conf['placeholder'])
while words:
task = rand.choice(words)
hangman(task, **conf)
print("You got it!")
words.remove(task)
if not input("Again? [y|n] ") == "y":
break
else:
print("Nothing left.")
####
CONFIG = {'percent': 25,
'placeholder': '_',
'prompt': "Lowercase letter? "}
if __name__ == '__main__':
main(CONFIG)
@horstjens
Copy link

das ist ....kurz....

@horstjens
Copy link

und genial zum Python keywords lernen :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment