Programming
Programming
Now you must put commands inside the conditional structure. The computer will
carry out these commands if the test is TRUE.
All the commands that follow the colon will be indented. That means they will be
set in from the left of the file window.
if answer==”Y”:
result=number1+number2
print(result)
The computer will only carry out the indented commands if the test is TRUE.
Example 1:
number1=70
number2=80
if answer==”Y”:
result=number1+number2
print(result)
Example 2:
if playerOne == playerTwo:
print('Wow! You both have the same name')
Example 3:
age = 15
To do the same thing in python, you enter the word else followed by a colon.
Example 1:
if answer==”Y”:
result=number1+number2
else:
result=number1-number2
print(result)
Example 2:
hourOfDay = 13
Loops
Every loop has an exit condition. The exit condition is how you stop the loop.
✓ A counter loop: (or fixed loop), repeats a set number of times then stops.
✓ A conditional loop(or condition-controlled loop), is controlled by a logical
test.
In python a counter loop is called a for loop. To make a loop that repeats 10 times
the command is:
For i in range(10):
for i in range(10):
num1=input("enter a number")
num1=int(num1)
num2=input("enter a number")
num2=int(num2)
result=num1+num2
print(result)
The commands inside the loop are indented. The indented commands will
repeat.
Increase a variable in python
This python program sets total to 20. Then increases the value of total by 10
Example 1:
total=20
total=total+10
print(total)
Example 2:
total=20
number=input("enter a number")
number=int(number)
total=total+number
print(total)
total=50
for i in range(5):
number=input("enter a number")
number=int(number)
total=total+number
print(total)
number=input("enter a number")
number=int(number)
while number!= 0:
total=total+number
number=input("enter a number")
number=int(number)
print(total)
total=total+1
print(total)
Activity, Extra Challenge, Test Page 105
4.5 Extend class project
Input a number
Program
total=0
visits=input("enter the number of visits")
visits=int(visits)
while visits =="Y":
visits=input("enter the number of visits")
visits=int(visits)
total=total+1
print(total)
Exit condition
Changing the exit condition. The loop will continue if the number of visits is
anything but 0. If the user enters the number 0 the loop will stop.
Program
total=0
visits=input("enter the number of visits")
visits=int(visits)
while visits != 0:
visits=input("enter the number of visits")
visits=int(visits)
total=total+1
print(total)
Activity, Extra Challenge, Test Page 109
4.6 Readable and user friendly
Some things that make a program easy to use are;
1. Simple inputs and outputs
Tips
✓ Make the input shorts and simple to enter the right inputs.
✓ Add a prompt. The prompt is the text inside the brackets.
✓ If you include a space at the end of the prompt, the interface looks better.
The program commands that handle inputs and outputs are called the interface.
Bad input: name=input(“name”)
Better version: name=input(“what is your name?”)
2. Clear outputs
Programs should end with an output.
✓ Bad output: print(total)
✓ Better version: print(“we saw this many birds” , total)