🔗 Link: https://awsrestart.instructure.com/courses/1632/modules/items/886826

📁 Repo: https://github.com/francopig/aws-python/tree/main/08. Trabajo con bucles

Lab overview

A loop is a segment of code that repeats. You will be introduced to two types of loops: the while loop and the for loop.

In this lab, you will:


Exercise 1: Working with a while loop

while loop makes a section of code repeat until a certain condition is met. In this exercise, you will create a Python script that asks the user to correctly guess a number.

Printing the game rules

  1. From the navigation pane of the IDE, choose the .py file that you created in the previous Creating your Python exercise file section.

  2. Use the print() function to inform the user about your game:

    print("Welcome to Guess the Number!")
    print("The rules are simple. I will think of a number, and you will try to guess it.")
    
  3. Save and run the file.

  4. Confirm that the script runs correctly and that the output displays as you expect it to.

Untitled

Importing random and writing a while loop

You will use the import command to include code that someone else wrote. Up to now, you have been using built-in functions. Recall that a function is a piece of reusable code.

  1. At the top of the file, include the Python module (which is a type of library) called random.

    Note: By convention, import statements are placed at the top of the script.

    import random
    
  2. Place the cursor on the next line after the second print() statement. Next, enter a statement that will generate a random number between 1 and 10 by using the randint() function of the random module.

    number = random.randint(1,10)