Python Guide #9: User Input

Python lets you accept user input, in this quick guide I will show you how to accept user input from within Python program.

name = input("Enter your name: ")
print("Your name is", name)

This quick example shows you how to accept user input. Execute program and see how it works.

With the knowledge who have so far, lets build some simple program, that will incorporate what I showed you so far.

We will create simple game.

Writing hangman game in Python for beginners

In this tutorial, we will create simple hangman game. For now you will play it with yourself, which we will improve on later, so do not delete this file after we finish.

First you input a word, that you or other person has to guess, then letters are covered. You need to guess letters, letter by letter. If you run out of chances, game ends and you lose.

In this simple program you will see what the word is, as it will be written in command line. This is just to visualise how it works

Lets begin:

# Lets give player 5 chances,
# you can modify this later if you want
chances = 5
word_to_guess = input("Input word to guess: ")
game_finished = False
failed_letters = ""
# functions len() returns length of an object
current_state = "_" * len(word_to_guess)

def update_state(letter):
    This function takes a letter as parameter
    and replaces underscore in current state
    in places where this letter should be
    new_state = ""
    for i in range(len(word_to_guess)):
        if word_to_guess[i] == letter:
            new_state += letter
            new_state += current_state[i]
    return new_state

# as long as game is not finished
# we want the cycle to repeat
# ask for letter
# update state
# evaluate chances
while not game_finished:
    print(current_state, "chances remaining", chances, "\nincorrect letters:\n", failed_letters)
    letter = input("Please input letter: ")

    # checking if letter is inside the word to guess
    # if it is right, we update the word
    # if not, we remove chance
    if letter in word_to_guess:
        current_state = update_state(letter)
    elif letter in failed_letters:
        print("You already used this letter!")
        failed_letters += letter
        chances -= 1
    # if current guessed word matches
    # word we need to guess
    # game is finished and we win
    if current_state == word_to_guess:
        print("Congratulations, you guessed the word:", word_to_guess)
        game_finished = True
    # if we have no chances left
    # we lost, game is finished
    # loop won't repeat
    if chances == 0:
        game_finished = True
        print("Word has not been guessed:", word_to_guess)

This program may seem a bit lengthy, but everything is commented out. Also it consists of elements we have already been talking about. Analyze the program yourself. Experiment with it. Once you understand it, do not delete it, as it will be used in the next guide.

Next: Python Guide #10: Importing Modules

Previous: Python Guide #8: Loops