Master Python Lists Basics to Create a Jumbled Game

What will we cover?

In this tutorial we will learn Python Lists basics. This will enable us to create a Jumbled Game.

Step 1: Python Lists and Indexing

A Python list is like a list you know it. And the beauty of Python lists is that they can contain anything.

But let’s get started immediately. You can define a list as follows. This list contains strings, but it could contain any types or objects.

my_list = ['Apple', 'Orange', 'Banana']

You get the length of a list by using len().

len(my_list)

Which will return 3.

A list is indexed from 0 – that is you get the first element as follows.

my_list[0]

The second element.

my_list[1]

And it continues as you can guess.

You can index from the end of a list by negative indexing – the last element is indexed by -1.

my_list[-1]

Then the second last element with.

my_list[-2]

Step 2: Get a random element from a list.

Remember the random library we used?

Well, it can be applied on a list.

import random
my_list = ['Apple', 'Orange', 'Banana']
random_item = random.choice(my_list)
print(random_item)

This will pick a random item from the list.

Step 3: Pick random samples from a sequence like a string

Imagine you want to get random samples from a sequence?

What is a sequence, well it can be a string. A string in Python is a sequence.

Then you can pick random samples from it.

letter_sequence = 'abcdefgh'
samples = random.sample(letter_sequence, len(letter_sequence))

Then samples will be a list of unique elements from letter_sequence. Hence, it is all the letters uniquely represented.

Step 4: The Jumbled Game Explained

The jumbled game can be described as follows.

  • A word jumble is a word puzzle game that presents the player with a bunch of mixed up letters and requires them to unscramble the letters to find the hidden word.
  • The computer will take word and jumble it (mix up the letters).
  • Then the player will guess what the word is
  • An initial word list could be: [‘father’, ‘enterprise’, ‘science’, ‘programming’, ‘resistance’, ‘fiction’, ‘condition’, ‘reverse’, ‘computer’, ‘python’]

Step 5: Implement the Jumbled Game

This is straight forward with our competencies.

import random
words = ['father', 'enterprise', 'science', 'programming', 'resistance', 'fiction', 'condition', 'reverse', 'computer', 'python']
word = random.choice(words) 
jumble = random.sample(word, len(word))
jumble = ''.join(jumble)
print(f"The jumble word is: {jumble}")
guess = input(f"Write your guess: ")
if guess.lower() == word:
    print(f"Corret! The {jumble} is {guess}")
else:
    print(f"Incorrect! The {jumble} is {word}")

Want more?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Rock-Scissor-Paper made Easy with Python – An Introduction to Randomness in Python

What will we cover?

How to use Randomness in Python and create a Rock-Scissor-Paper game.

Step 1: Randomness in Python

To make games interesting you need some unpredictable aspects. This is where randomness is used.

Luckily, Python has a library to make randomness. To simulate rolling a die can be done as follows.

import random
die = random.randint(1, 6)
print(die)

Where randint(1, 6) returns a random integer from 1 to 6, both inclusive.

Step 2: The Rock-Scissor-Paper game

I think most know the game. If not, read the rules on wiki.

Let’s try a game of it.

print("Enter choice \n 1. Rock \n 2. Paper \n 3. Scissor \n")
choice = int(input("Choice: "))
computer_choice = random.randint(1, 3)
if choice == computer_choice:
    print("Draw")
elif choice == 1:
    if computer_choice == 2:
        print("Computer wins, Paper")
    else:
        print("You win, Scissor")
elif choice == 2:
    if computer_choice == 1:
        print("You win, Rock")
    else:
        print("Computer wins, Scissor")
elif choice == 3:
    if computer_choice == 1:
        print("Computer wins, Rock")
    else:
        print("You win, Paper")

Step 3: What is Next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Program Flows in Python – Conditional Statements and Boolean Expressions Controls the Program Flow

What will we cover?

We will cover what a program flow is, how this links to conditional statements and boolean expressions.

Step 1: Let’s recall what boolean expressions are

Boolean expression is the building block of conditional statements. So let’s make a brief recap of it.

Given two variables a and b, we have the following boolean expressions with them.

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

Where depending on the values in a and b the expressions will evaluate to True of False.

Step 2: Conditional statements

Let’s demonstrate a few if-statements here.

Equality between variables.

a = 10
b = 10
if a == b:
    print("a equals b")

Notice, that if a was not equal b, then it would not print the statement. That is, the indented code after the if-statement, is only executed if the boolean expression evaluates to True.

Another example with less-than.

a = 10
b = 8
if a < b:
    print("a is less than b")

Again the same principles applies.

You can also have an else-clause.

a = 12
b = 12
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")

And a elif-clause.

a = 13
b = 12
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is less than b")

And a not-equal statement.

a = 10
b = 10
if a != b:
    print("a does not equal b")

What next?

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.