🔗 Link: https://awsrestart.instructure.com/courses/1632/modules/items/886826
📁 Repo: https://github.com/francopig/aws-python/tree/main/08. Trabajo con bucles
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:
while
loopfor
loopA 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.
From the navigation pane of the IDE, choose the .py file that you created in the previous Creating your Python exercise file section.
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.")
Save and run the file.
Confirm that the script runs correctly and that the output displays as you expect it to.
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.
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
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)