Let's build a simple game!

This exercise consists of two parts. In the first part, we will review building a list containing ten random numbers between 0 and 10. The second part is a challenge to the reader. Perform the following steps:

  1. The first step is creating an empty list. Let's create an empty list called random_list. An empty list can be created as follows:
       random_list = []
  1. We will be making use of Python's random module (https://docs.python.org/3/library/random.html) to generate random numbers. In order to generate random numbers between 0 and 10, we will make use of the randint() method from the random module:
       random_number = random.randint(0,10)
  1. Let's append the generated number to the list. This operation is repeated 10 times using a for loop:
       for index in range(0,10):
random_number = random.randint(0, 10)
random_list.append(random_number)
print("The items in random_list are ")
print(random_list)
  1. The generated list looks something like this:
       The items in random_list are
[2, 1, 0, 8, 3, 1, 10, 9, 5, 4]

We discussed generating a list of random numbers. The next step is taking user input where we ask the user to make a guess for a number between 0 and 10. If the number is a member of the list, the message Your guess is correct is printed to the screen, else, the message Sorry! Your guess is incorrect is printed. We leave the second part as a challenge to the reader. Get started with the list_generator.py code sample available for download with this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.172.115