DEV Community

Cover image for A Simple Python User Interaction Game
Juwon?🍀
Juwon?🍀

Posted on • Updated on

A Simple Python User Interaction Game

In this article, I will discuss how to use Python to create a simple user interaction. As you implement this code, you will gain a deeper understanding of Python programming.

  • Input and Output Handling: The game requires handling of user's input and displaying output to the user.
  • Control Flow: The game requires conditional statements and loops to control the game flow based on user input and game logic.
  • Debugging: Debugging is a crucial skill in programming. Implementing a simple user interaction code can help programmers practice debugging by identifying and fixing an error in the code.
  • User Interface Design: Although a simple program such as this does not require complex user interfaces, it can still teach a programmer the essentials of designing user-friendly interfaces for their programs.
  • Code organization: The program requires the organization of code into functions and modules, which can help the programmer learn how to write modular and reusable code.

Prerequisites

Before creating the Python simple user interface, ensure the following is installed on your system:

  • Operating systems such as Windows, Linux, or MacOS
  • Python installed on your system(updated version)
  • A code editor or IDE(Visual Studio code with Jupyter notebooks extension)

Project Description

This article will describe the program in detail:

  • Display a list
  • Have a user choose an index position and an input value
  • Replace value at index position with user's input value

Step 1: Displaying List
Let's start by creating the display function.

Creating the game list

#Python
#game list that would be passed into the display function

game_list = ['0','1','2']
Enter fullscreen mode Exit fullscreen mode

Creating the display function

#Python
#A function that displays the game list

def display_list(game_list):
    print('Here is my current list')
    print(game_list)

print(display_list(game_list))
Enter fullscreen mode Exit fullscreen mode

Output of the display function

#Code output

Here is my current list
['0', '1', '2']
None
Enter fullscreen mode Exit fullscreen mode

Step 2: Position Choice
In this step, I would create a function that asks the user for a position. I would use the while loop to keep asking the user for inputs in case the user is entering a position out of range or a string.

Creating the position choice function

#Python
#A function that asks the user for input to determine the index position on the list 


def position_choice():

    #This original choice value can be anything that isn't an integer
    choice = ''

    #While choice is not a digit keep asking for input.
    while choice not in ['0','1','2']:
        choice = input('Pick a position (0,1 or 2): ')
        if choice not in ['0','1','2']:
            print('Sorry, Invalid choice!')

    return int(choice)

print(position_choice())
Enter fullscreen mode Exit fullscreen mode

Output of the position choice function

#code output

Pick a position(0,1,2): two
Sorry, Invalid choice!
Pick a position(0,1,2): 24
Sorry, Invalid choice!
Pick a position(0,1,2):2
Enter fullscreen mode Exit fullscreen mode

In the output above, I inputted the 'string two' and the 'number 24'. The string two is not a number, so the code continues, while the number 24 is out of range. I finally input the correct value '2' and the code runs perfectly.

Step 3: Replacement Value
In this step, I would create a function that replaces value in the game_list. This function allows the user to replace an item in the game_list with the chosen index position.

Creating the replacement function

#Python
#A function that asks for a replacement value

def replacement_choice(game_list, position):
    user_placement = input('Type a string to be replaced: ')
    game_list[position] = user_placement

    return game_list

print(replacement_choice(game_list,1))
Enter fullscreen mode Exit fullscreen mode

Output of the replacement function

#code output

Type a string to be replaced: two
['0', 'two', '2']
Enter fullscreen mode Exit fullscreen mode

In the output above, we can see that when the function was executed, it asked the user to input a string.
From the function, we can see it took in the game_list and position as attributes already.
The user input was replaced at the index position passed into the function with the list.

Step 4: Replay Choice
In this step, I would create a function that asks the player whether to keep playing.

Creating the replay function

#Python
#A function that asks for replay value

def gameon_choice():
    choice = ''

    while choice not in ['Y','N']:
        choice = input('Keep playing, please choose (Y or N): ').capitalize()
        if choice not in ['Y','N']:

            print('Sorry I dont understand, choose Y or N!')

    if choice == 'Y':
        return True
    else:
        return False

print(gameon_choice())
Enter fullscreen mode Exit fullscreen mode

Output of the replay function

#code output

Keep playing, please choose (Y or N): Y
True
Enter fullscreen mode Exit fullscreen mode

Step 5: Game Logic
This is the final step. In this step, we would arrange our functions and apply logic with conditional statements to allow the game to work as we want it to.

Creating the Game Logic

#Python
#Game Logic

game_on = True
game_list = ['0','1','2']

while game_on:
    display_list(game_list)

    position = position_choice()

    game_list = replacement_choice(game_list, position)

    display_list(game_list)

    game_on = gameon_choice()


Enter fullscreen mode Exit fullscreen mode

Output of final code

#code output

Here is my current list
['0', '1', '2']
Pick a position(0,1 or 2): two
Sorry, Invalid choice!
Pick a position(0,1 or 2): 0
Type a string to be replaced: zero
Here is my current list
['zero', '1', '2']
Keep playing, please choose (Y or N):q
Sorry I don't understand, choose Y or N!
Keep playing, please choose (Y or N): N
Enter fullscreen mode Exit fullscreen mode

Using While Loop
The while loop allows for a continual loop until a certain condition is met. In this case, the loop kept asking for valid input until the user chose a number or chose to end the game, which ended the loop. The code then replaced the value at index position '0' with the string 'zero' that was inputted by the user and displayed the updated list. Finally, the user was asked if he wanted to continue the game, the user chose the wrong input at first and was prompted again by the game, the user chose 'N' and the loop ended and the game was over.

Conclusion
The article is about the implementation of a simple user interaction game, which I have broken down step by step for easy readability. I hope it's educational. It would be greatly appreciated if you followed me, read my previous articles, share your honest opinions, and react and comment.

Latest comments (0)