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.

Learn Python

Python Like a Pro?

If you’re serious about learning Python, there’s nothing better than strong commits. At your request, we have created an improved version of this popular free online course.

This version has the following benefits to enhance your learning journey.

  1.  Tracking your progress in the course.
  2.  Questionaries to ensure you understand concepts between important lessons.
  3. Downloadable Cheat Sheets for fast lookup what you learned.
  4. Direct Q&A with the instructor to help you to understand the material better.
  5. Added material for better explanations and insider knowledge.
  6. Extra videos with more explanations and stories.
  7.  Certificate at completion.

Start the change in your life and commit to doing something amazing that you have always dreamed of.

Sign up and become part of the exclusive Python Like a Pro elite.

Leave a Reply Cancel reply

Exit mobile version