Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
In this example we are going to start writing programs that make decisions. A sample program is where you are asked a question and the computer replies to your ...
Typology: Study Guides, Projects, Research
1 / 20
We are going to create the program which asks you what your name is and then says hello to
you, using the name you entered.
Start Python, inside the Python Shell go to File and select New File. This stage is very
important. By creating your program in a new file, it lets you save your code so you can edit it
or come back to it later. Write the code shown below.
#First Python Program #Your name #Today’s date Name = “” print('What is your name? ') name = str(input()) print('hello ', name)
Python will not let you execute a program until it is saved.
To run the program, from Run menu select Run Module
or press F5 key. The program will run in the Python
Shell window.
We are going to create the program which asks you what your name and favourite colour is
and then says hello to you, using the name you entered, and tells you what colour you entered.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Using Variables #Your name #Today’s date Name = “” Colour = “” print('What is your name? ') name = str(input()) print('What is your favourite colour? ') colour = str(input()) print('hello ', name, ' your favourite colour is ', colour)
Run the program and enter your name and favourite colour.
In the example shown here you are being asked how many hours per week you work and how
much you get paid per hour.
The program then works out how much you earn in one week from the information given.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Arithmetic Operations #Your name #Today’s date hours = 0 rateOfPay = 0. weeklyPay = 0. print('How many hours do you work? ') hours = int(input()) print('What is your rate of pay? ') rateOfPay = int(input()) weeklyPay = hours * rateOfPay
print('your weekly pay is £', weeklyPay)
Run the program and test it is working, enter 30 hours and £8 rate of pay.
Program 1 Write a program that asks the following questions:
What is the price of a train ticket from Glasgow to London? How many tickets do you require?
The program then reports back how much this is going to cost in total.
Program 2 Write a program that asks the following questions:
How many classrooms are in the school? What is the maximum number of pupils who can fit into one classroom?
The program then reports back the maximum number of pupils the school can accommodate.
Program 3 Write a program that asks the following questions:
How many pupils are in the lunch hall? How many boys are in the lunch hall?
The program should then report back how many girls are in the lunch hall.
Program 4 Write a program that asks the price of an air flight to Australia.
The program should then report back how much this will cost if the person travelling is allowed 20% discount.
In this example we are going to start using a variable to keep a total. The idea here is that
every time a new value is entered the total increases by that value.
A simple example is a game of darts, where a player is given three darts and the total is the
sum of the three scores. Enter
Inside the Python Shell go to File and select New File. Write the code shown below.
#Keeping a running total #Your name #Today’s date
#Initialise variables total = 0 number1 = 0 number2= 0 number3 = 0 print('What is your first score? ') number1 = int ( input()) total = (total + number1) print('What is your second score? ') number2 = int ( input()) total = (total + number2) print ('What is your third score? ') number3 = int ( input()) total = (total + number3)
print ('your total score was ', total)
Run the program and test it is working, enter the following dart score: 80, 19, 85
The total should be: 284
Program 1 Write a program that asks two people what their ages are.
The program then reports back what their total age is.
Program 2 Write a program that asks four people what their scores in an archery tournament were.
The program then reports back what their total score was.
Program 3 Write a program that asks the price of apples, bananas, cherries, grapes and pears.
The program then reports back what the total of these five items is.
Program 4 Write a program that asks three people their height in centimetres.
The program then reports back what their total height is.
In this example we are going to start writing programs that make decisions.
A sample program is where you are asked a question and the computer replies to your answer.
For example, the program on this page asks you to enter a name and an age. If the age
is less than 18 the program sends the message ‘You are not old enough to vote’.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Selection with IF #Your name #Today’s date
Name = “” Age = 0 print( 'What is your name? ' ) name = str( input()) print( 'What is your age? ' )) age = int ( input()) if age < 18: print (name, 'you are not old enough to vote' )
Test the program by entering your name and age, the program should display the message.
Now test the program by entering ‘Jane’ and 21, the program does not display a message.
In the code above if you do not indent print(name, 'you are…)Python will not run and an error message will be displayed on the screen.
Program 1 Write a program which asks you what your score in a Computing test out of 30 is (The pass mark is 15).
The program then reports if you have passed the exam.
Program 2 Write a program which asks you to enter your age.
The program then reports back whether you are a senior pupil (age >=16 and age <=18)
Program 3 Write a program which asks you to enter 3 marks between 1 and 30, calculates the total then works out the average of the marks. average = total/
The program then reports back if your marks are above average (45).
Program 4 Write a program which asks you to enter 4 amounts of money between £1 and £50. The program adds up these numbers.
The program then reports back that you get 20% discount if the total is greater than £35.
A sample program is where you are asked a question and the computer replies to your answer.
For example, the program on this page asks you what the capital of Spain is. If you
guess correctly the computer says ‘Well Done’, if not the computer tells you ‘That is not
Correct’.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Selection with IF - Else #Your name #Today’s date Guess = “” print( 'What is the capital of Spain? ' ) guess = str( input()) if guess == 'madrid' : print ('Well done!') else : print ( 'That is not correct' )
Test the program by entering madrid, the program should display ‘Well done!’
Run the program again this time enter a wrong answer, the program should display ‘That is not correct’.
Program 1 Write a program which asks you what your score in a Maths test out of 100 is. (The pass mark is 50)
The program then reports if you have passed or failed the exam.
Program 2 Write a program which asks you to enter your age.
The program then reports back whether you are a junior (age < 16) or a senior (age >=16)
Program 3 Write a program which asks you to enter 4 numbers between 1 and 20 and then works out the total of the numbers.
The program then reports back 'Too High' if the total is over 40 and 'Too Low' if the total is under 18.
Program 4 Write a program which asks you to enter 3 amounts of money between £1 and £20. The program adds up these numbers.
The program then reports back that you get 20% discount if the total is greater than £ otherwise it will report that you get 10% discount.
In this example we are going to start using a fixed loop in a program. A fixed loop allows us
to repeat some lines of code many times. A fixed loop is used when the number of times you
want the code to repeat is known.
A simple example is where we want to ask 4 people their name and display a message to
them.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Fixed loops #Your name #Today’s date
Name = “”
for x in range (4): print('What is your name? ') name = str(input()) print ('hello, ', name)
Test the program by entering four names.
Now change the code to the following:
Name = “” print('What is your name? ') name = str( input ()) for x in range (10): print (x, ' hello, ' , name)
Program 1 Write a program which uses a fixed loop that asks five people their names and ages.
The program then reports back the name and age of each person.
Program 2 Write a program which uses a fixed loop where a character asks three people their name and
what their scores in an archery tournament were.
The program then reports back name and score of each person.
The following program will ask you to enter the number of times table, (if you want to display 7 times table, 7 would be the number entered by the user). The program will then calculate and display the times table chosen.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Fixed loops with counter #Your name #Today’s date
counter = 1 result = 0 num = 0 print( 'enter table number ' ) num = int ( input()) for x in range(12): result = num * counter print (num, ' x ' , counter, ' = ' , result) counter = counter + 1
Test the program by entering the number 7.
The value of counter variable is set to 1 and result variable set to 0
The value in number variable is 7 (assuming this is entered by the user)
The code inside the fixed loop will be repeated 12 times.
The first time the loop is executed, the value placed in result variable is 7. counter (1) * number (7). The program then displays the number (7) x counter (1) = result (7) One is added to the variable counter, which now has the value 2. The calculation is worked out first and the value is then placed in the variable.
The second time the loop is executed, the value placed in result variable is 14.
counter (2) * number (7).
It will then display the number (7) x counter (2) = result (14)
And so on… until the code has repeated 12 times.
In this program we want to enter 5 scores, add them up as we enter them and display the total.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Fixed loops with running total #Your name #Today’s date
total = 0 result = 0 score = 0 for x in range (5): print( 'enter a number between 1 and 20 ' ) score = int ( input()) total = total + score print ( ‘Your total score was ' , total)
Test the program by enter the following scores – 19, 13, 12, 17 and 16.
Total is set to 0 The code inside the fixed loop will be repeated 5 times.
The first time the loop is executed, the value placed in the variable score is 19. Score (19) is added to total (0), (19 + 0) and the value (19) is now placed into total variable.
The second time the loop is executed, the value placed in the variable score is 13. Score (13) is added to total (19), (19 + 13) and the value (32) is now placed into total variable. . The third time the loop is executed, the value placed in the variable score is 12. Score (12) is added to total (32), (32+ 12) and the value (44) is now placed into total variable.
And so on… until the code has repeated 5 times, the total is then displayed.
Program 1 Write a program which uses a fixed loop that asks six people what their ages are.
The program then reports back what their total age is.
Program 2 Write a program which uses a fixed loop that asks four people what their scores in an archery tournament were.
The program then reports back what their total score was.
Program 3 Write a program which uses a fixed loop that asks you to enter how many pieces of fruit you ate each day last week. (Think: How many times must the loop repeat?)
The program then reports back what the total pieces of fruit you eat each week.
Program 4 Write a program which uses a fixed loop that asks three people their height in centimetres.
The program then reports back what their total height is.
In this section we will look again at loops. This time the number of times a loop is to be run is not known. A conditional loop will keep on running until a certain condition is met.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Conditional loop #Your name #Today’s date
total = 0 number = 0 while total < 100: print( 'enter a number ' )) number = int ( input()) total = total + number print (total, ‘ is more than 100 ' )
Test the program by entering the following numbers: 45, 12, 31 and 15
Test it a few more times inputting your own numbers.
Program 1
Write a program that asks you to enter a series of numbers and stops when you have entered six 6s. (ie when you have entered the number 6, six times)
Program 2
Write a program that asks you to enter a positive number. If you enter a negative number you will be given an error message and asked to enter it again. (You should test this program by entering both positive and negative numbers)
Program 3
Write a program that asks you to enter an exam mark out of 50. If you enter a mark less than 0 or greater than 50 you will be given an error message and asked to re-enter the number. (Hint: Use an OR operator)
Program 4
Write a program that asks you to enter a user name and a password. If you do not enter the correct user name and password you are asked to re-enter both of them. (Hint: Use an AND operator)
(The user name is COMPUTING - The password is PYTHON99)
In this section we will find out how to create random numbers.
Many programs (lottery, bingo etc) rely on random numbers being selected by the computer. We will use the following code.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Random Numbers #Your name #Today’s date number = 0 import random for x in range (6): number = random.randint(1,10) print ( ‘random number is ‘, number)
Run the program, it should display 6 random numbers between 1 and 10.
Program 1 Write a program that generates a set of random numbers between 1 and 10 and stops when the number is a six.
Program 2 Write a program that generates a random number between 1 and 12. If the number produced is a seven you should display a message on screen saying 'you have won the jackpot!'
Program 3 Write a program that generates 6 random numbers between 1 and 49 and displays them with a 2 second interval between each one.
Program 4 Write a program that generates four random numbers between 1 and 6. The program should print the total of the numbers. If the total is greater than 13 then the message 'High Score' should appear. If the total is less than 13 the message 'Low score' should appear.