Book 1 Python Programming Challenges
PythonProgrammingChallenges
NameClass / Teacher
Target Grade
- Complete the CodeCademy course listed on each page
- Complete the challenge on the page
- If you are a level 6 – you need to do the extension at the bottom of the page as well.
- If you are a level 8 – then you need to do all the extension at the bottom of the page as well.
- Save your code as “challenge 1” or “Challenge 1 L6”
Yourresponse
Completedsuccessfully? / Yesorno. How tough was it?
Didyouhaveanyerrors? / Yes– syntaxerrors(writeintheerror)orNo
What new skill have you learnt / Mention the new programming skills you have learnt during this challenge.
code
Write your own challenge to show understanding this skill / Can you write a challenge to test other students skills.
- Lookat theprogramsthatyouhavecompletedin previouslessonsforhelp
- Use the SHS Computing website
- Askforhelpfromothers
- Completethemin sequenceanddonot moveontothenextchallengeuntilyouhave successfullygottheprogramrunning AND UPDATED THE REVIEW GRID.
If you get stuck – look for the green box – it might be able to help you.
Python 1 of 20
Book 1 Python Programming Challenges
Complete these code Academy courses BEFORE taking these challengesPython Syntax
Challenge1
Practicetask:
WritethisprograminPython:
print(“Hello World!”)
Challenge:
Writeaprogramthat says:
Thisis
acomputer program
thatprints onseveral lines
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Completed successfully?
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6Challenge:
Complete the above challenge only using 1 print statement.
Grade 8Challenge:
Print the following output on 1 line, using 3 separate print statements.
Hello world, I think programming is fun
Challenge2
CodeCcademycourse to be completed BEFORE taking this challengePython Syntax
Practicetask:
Copyand runthefollowingprogram:
name=”Bob”
print(“Hello “,name)
Challengetask:
Completethefollowing programsoit uses6variablesto printout‘thecatsat on themat’.Itshould print out on oneline,withspaces.
E.g.
word1=”the”
word2=”cat”
word3=”sat”
[REMAINDEROFPROGRAMHERE]
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Complete the above challenge using 6 print statements
Grade 8 Challenge:
Write you own nursery rhyme using variables and print statements. Be sure to include some apostrophes, speech marks and <tab> characters.
Grade 9 Challenge:
Using print statements, print out the 3 times table
Python 1 of 20
Book 1 Python Programming Challenges
Challenge3
CodeCcademy course to be completed BEFORE taking this challengeSKIP Tip Calculator
Strings& ConsoleOutput
Practicetask:
Copy andrunthefollowingprogram
print(1+6)
print(7-5)
print(3*9)
print(7/2)
Challengetask:
Writeaprogramthathas3variables (a,band c).a=12 and b = 6. cshould beequaltoaplus b.Theprogram shouldprint the value of variable c.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Write a program to calculate and print the area of a triangle, which can be calculated using the formula:
( perpendicular height * width ) / 2
You will need 2 variables.
Grade 8 Challenge:
Write a program to calculate and print the area of a circle, which can be calculated using the formula:
pi * radius * radius
Pi is 3.14, and you will need 2 variables
Python 1 of 20
Book 1 Python Programming Challenges
Challenge 4
CodeCcademy course to be completed BEFORE taking this challengeStrings &
Console Output
Practicetask:
Copy andrunthefollowingprogram:
name=input(“What is your name?”)
print(“Hello “ +name)
Challengetask:
Writeaprogramthatasks theuser theirnameandthenasks whattheirfavouritefood is,usingtheir namein thequestion,andrespondstotheiranswer.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Write aninteractive program that asks the user for their name, the using their name asks them for their age, and using their name and age asks them for their favourite subject.
Grade 8 Challenge:
Write a program that asks the user for their 5 favourite animals. Then it prints the last 5 lines of “12 days of Christmas” using the favourite animals.
Python 1 of 20
Book 1 Python Programming Challenges
Challenge5
CodeCcademy course to be completed BEFORE taking this challengeStrings &
Console Output
Writeaprogramthatasks theuser theirfirstnameand thenasks themtheirsurnameandthen prints theirwholename3times.
Eg:
Whatis your first name? Mr
Whatis your surname? Martin
Mr Martin Mr Martin Mr Martin
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Writeaprogramthatuses thefollowingvariables tocalculatethenumberof minutesin aweek: The number of minutes can be calculates as (MinutesPerHour) * (HoursPerDay) * (DaysPerWeek)
You will need 3 Variables named:DaysPerWeek HoursPerDay MinutesPerHour
Grade 8 Challenge:
Thomas the tank engine uses 100kg of coal per mile of travel when not pulling any carriages. Each carriage uses an additional 10kg of coal per mile. Write a program to ask the user how many carriages the complete train will have and the distance to be travelled and print the amount of coal to be loaded onto Thomas for the trip.
Python 1 of 20
Book 1 Python Programming Challenges
CodeCcademy course to be completed BEFORE taking this challengeStrings &
Console Output
Challenge6
TypeCasting
Changing the data type from one type to another is called casting. Tocastdatatoanintegerweuseint() Tocastdatatoastringweusestr()
Challenge:
Write a program that asks the user their name, says ‘Hello’ to them, using their name and then asks how old they are. The program should then calculate their age at their next birthday and respond “So yournext birthday you are (age)?’
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Remember the .lower() & .upper() methods from last lesson. Open your program from challenge 4, extension challenge 2 (the one about the 12 days of Christmas. Amend this program so that it uses .lower() on the odd numbered output and .upper() on the even numbered outputs.
Grade 8 Challenge:
Findtheareaofarectangle. Writeaprogramthatfindsthe areaof arectangle: Eg:
Please enter width: 9
Please enter height:5
The area is: 45
CodeCcademy course to be completed BEFORE taking this challengeDate and time
Challenge7
Rememberthatinput()alwaysreturns astring.Youneedtouse typecastingtoconvertastring(str)toan integer (int) orinteger toastring.
Writeaprogramthat helpstheuser toaddupthecoins in their piggybank.The programshouldask: “Howmanypennies”, “Howmanytwopences”,“Howmany5pences” etc. Then itshouldgivethe totalvalue ofthe piggybank and todays date.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Rememberthatprogramscanmakedecisionsbasedontheinput oftheuser–itchecksIF a conditionismetandIF
notitcandosomethingELSE.
Tocomparevaluesweneedtousecomparisonoperatorssuchas:==!=
Developtheprogramsothatifthe userssavingsadduptoless than£50ittellsthemtosave moreandifitisover£50ittellsthem thatthey’redoingwell.
Grade 8 Challenge:
Enhance this program to calculate how much more money they need to reach £150 and how long they have until Christmas to reach this target.
Challenge8
CodeCcademy course to be completed BEFORE taking this challengeConditionals &
Control Flow
If..else
Python uses if else statement as follows
If day == “Saturday” or day == ”Sunday”:
print “At least I can sleep in today”
else
print “Oh, No programming today – thats sad”
Challenge
Writeaprogramthat askstheuserhowlong,on average,theyspend onacomputerperday.
Iftheuserspendslessthan2hoursperday,theprogramneedstosay,‘Thatseemsreasonable’ ELSE, theprogramneedstosay,‘Getalife’
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Extend the program above to ask the user about other PC time, such as smart phone, ipad, xbox, ps3 time and add this to the amount of time they spend on the computer
Grade 8 Challenge:
Extend the program again to IF….ELIF…..ELSE. The program should say suitable messages for less than 2 hours per day, between 2 and 3 hours and more than 3 hours.
CodeCcademy course to be completed BEFORE taking this challengeConditionals &
Control Flow
Challenge9
A shop is having a sale. They’re giving 10% off when a customer spends £10 or less and 20% off when they spend over £10. Write a program that asks for the amount spent and then displays the discount to be applied and then the final price (ie with the discount applied)
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Extend the program above to ask the customer if they have a store (loyalty) card. Give them double discount if they do have such a card.
Grade 8 Challenge:
Extend the program again to give everyone a 5% discount if they are shopping on a weekday.
CodeCcademy course to be completed BEFORE taking this challengeConditionals &
Control Flow
Challenge10
If..elif…else
The code:
# Program to ask the user for their name and exam mark %.
# It should then print their grade based on
# >90% A*, >80% A, >70% B, >60% C, 60% or less F.
# ask the user for their name
name = input("Enter you name ")
# Ask for the use for their exam mark and convert it to an integer
mark = int(input("what mark did you get? "))
Challenge
Add to this code to make it print out the exam grade based on the mark % the user enters. You will need to use if elif else statements.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Extend your answer above to print a message if the student is above their target grade. You’ll need to ask the user for their target grade.
Grade 8 Challenge:
Extend challenge 1 above to print a message based on how the student is doing compared to their target grade.
Challenge11
CodeCcademy course to be completed BEFORE taking this challengeConditionals &
Control Flow
If..elif…else, upper(), variables.
# Program to ask the user for their name.
# It should then pose the user 3 questions and ask for answer
# and tell the user if they are right or wrong.
# ask the user for their name
name = input("Enter you name ")
# Pose the question 1
a1 = input ("Q1. which football team plays at Upton Park? ")
if a1 == "West Ham United":
print ("Correct")
The code poses a question and lets the user provide and answer. If they get it right they are told so.
Challenge
Complete the program to ask a total of 3 questions and tell the user the correct answer if they provide the wrong one. You can also change the first question if you wish.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Extend the solution to the program to ignore capital letters in the answers. Thus in the above example all the following “West Ham United”, “WEST ham United”, “west ham united” will all be marked as correct. You might want to use .lower() or .upper()
Grade 8 Challenge:
Extend challenge 1 above to count how many answers the user gets right and displays a summary of the correct number of marks at the end of the quiz.
CodeCcademy course to be completed BEFORE taking this challengeLoops
Challenge 12
Import Statement
The statement
import time
allows you to use time features in python. One of these is time.sleep(5) which will lets your program sleep (or wait) for 5 seconds.
Challenge
Writeaprogramthatasks theuser‘Whatisyourquestion?’Theprogramshould respondwith‘Letmethink aboutthat’ and pausesfor10 secondsbeforeresponding with ‘Idon’t wanttoanswerthatquestionrightnow’
Theprogramshould thenask theuserthe questionthattheuserentered, wait for an input,and thenlaugh outloud(lol) attheirresponse.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Copythefollowingprogram:
importrandom
print(random.randint(0,100))
Therandom moduleletsyougeneraterandomnumbers.
Writeaprogramthat prints outarandomnumberevery3secondsfor 300seconds
Python 1 of 20
Book 1 Python Programming Challenges
CodeCcademy course to be completed BEFORE taking this challengeSKIP PygLatin
Functions
Challenge13
Write a program that asks the user 5 numbers, you will need to store them and then prints the smallest and largest number in the list.
Hint: You will need a function that has 2 parameters and returns the smaller of the 2. You will also need a second function that returns the larger of 2 numbers.
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
Extend the program above to take the absolute value of the numbers before finding the smallest and largest.
Grade 8 Challenge:
Complete Lesson 13 on CodeCademy
SKIP Taking aVacation
Python Lists
and Dictionaries
SKIP A Day at theSupermarket
SKIP StudentBecomes the
Teacher
SKIP Lists andFunctions
SKIP Battleship!
Loops
Challenge14
For Loop…
Copy thecodebelowintoPythonandsee whatitdoes:
for i in range(0,5):
print (str(i) + " looping")
Oneof thethingsthatmakescomputerssouseful isthespeed atwhichtheydothings. Thismakesthem useful for whenwe wantthemtodosomethingoverandover.We callthis iterationorlooping.
Challenge:
Writeaprogramthatcountsfrom 1 to3witha1secondpausein betweenthenumbersand thensays ‘GO!’
SelfreviewHow difficult did you find this Challenge /
Done with my eyes closed /
Had to think about it /
Wow – that was tough
Did you have any errors? What?
How did you solve them?
What new skill have you learnt
Write your own challenge to show understanding this skill
Grade 6 Challenge:
For loops are good at doing something a set number of times, like times table. Write a program to print out the 6 times table.
Grade 8 Challenge:
For loops can be placed inside one another, such as
for x in range ( 1, 5 ):
for y in range ( 10, 15 ):
print (x, y, "hello – this will get printed lots of times")
Write down what you think will happen, and why.
CodeCcademy course to be completed BEFORE taking this challengeSKIP Taking aVacation
Python Lists
and Dictionaries
SKIP A Day at theSupermarket
SKIP StudentBecomes the
Teacher
SKIP Lists andFunctions
SKIP Battleship!
Loops
Challenge15
Ghost.
There is a ghost hidden behind 1 of 3 doors. Write a program to choose a door at random and ask the user to guess which door the ghost is behind. The idea is for the user to avoid the ghost. If the user has avoided the ghost then add 1 to their score, and they have another go as the ghost may have moved doors. If the user has found the ghost then their go is over – print out how many times they missed the ghost.