Variables and Data types
Create your own Program
Set your Learning Objectives to red.
Open your PowerPoint from last lesson.
If you need to download a copy of thePowerPoint Click here and open Task 1.
In Python The 'if' statement is used to conditionally execute a statement. In other words we look at a statement(conditional) and decide if that conditional statement is true or false,we then do one thing when the condition is true and something else when the condition is false.
Lets see if we can create a version of the above flowchart in python.
Notice how when we compare if things are equal in python we use == not a single =.
We use conditional statements in a python if statement.
Look at the conditional statement in the diamond below, should we DO THIS or DO THAT?
What about this next one, should we DO THIS or DO THAT?
Copy the text below into a Python file and run it to see what happens
Can you change the code so that is has a user input asking for the number and an else statement in case the number is not greater that 5
Explain your answers in your Powerpoint next to a screenshot of your code
Can you change the condition to check if the two numbers are the same and print out suitable statements?
Sometimes we want to have more that two opions when we make a choice we can do that in python by using an elif statement.
1. Can you change the code so that the user inputs a number and you then print out whether it is small medium big or huge?
2. Can you develop the code so that the user can input their favourite colour and you then print out something about the colour they chose?
In Python, the elif statement is used for multiple conditions in a decision-making structure. It stands for "else if," and it comes after an if statement and before an else statement.
elif
if
else
Here is an example of how elif is used:
if temperature > 30: print("It's a hot day.") elif temperature > 20: print("It's a nice day.") elif temperature > 10: print("It's a bit cold.") else: print("It's cold.")
In this example, Python checks each condition in order. If a condition is true, it runs the code inside that block and skips the rest.
Write a Python program using if, elif, and else statements to recommend an outfit based on the temperature.
Here's a structure to help you start:
temperature = # Add a temperature value here # Write your if, elif, and else statements below
Once you've written your program, test it with different temperatures to see the outfit recommendations.
Overall objective: In this task, you will use your knowledge of Python's if statement to create your first choices for your text adventure game . "
You stand at the edge of a dense, ancient forest, its towering trees shrouded in mist that dances in the pale moonlight. The air is thick with the earthy scent of moss and the distant murmur of a hidden stream. Before you lies a forgotten path, overgrown with ivy and covered in fallen leaves, disappearing into the heart of the woods. It beckons you with an eerie allure, promising secrets and treasures untold. But be warned, brave adventurer, for this forest holds mysteries of both wonder and peril. Will you:
Tips:
Once you've completed both parts of this task, you will have crafted a captivating introduction and created an interactive text adventure game, showcasing your creative writing skills and your ability to use variables and input in Python.