python - complete - копия - копия
python - complete - копия - копия
python - complete - копия - копия
For KS3/4
© Zi gZag Education, 2014 Thi s resource may only be copied by the purchasing i nstitution on a single site and for their own use
Python Challenge 1
Challenge 1
You will need to use:
Write a program that:
• asks the user to input their age Sequencing
• outputs 'Your age is: ' followed by their
age
Variables
Difficulty:
© Zi gZag Education, 2014
1
Python Challenge 1
Challenge 1: Solution
#!/usr/bin/python3
print("----MATHS QUIZ----\n")
print("----SCORES----")
#Creates the scores.txt file if it doesn't exist
file = open("scores.txt", "a")
file.close()
#Opens the file in read-only mode
file = open("scores.txt", "r")
#Loop that prints each line from the file to the
screen
for line in file:
print(line)
file.close()
Difficulty:
© Zi gZag Education, 2014
1
Python Challenge 2
Challenge 2: Solution
#!/usr/bin/python3
Tip: Multiply the width by the height to find the area of a rectangle.
Difficulty:
© Zi gZag Education, 2014
1
Python Challenge 3
Challenge 3: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
1
Python Challenge 4
Challenge 4: Solution
#!/usr/bin/python3
print ("----DIVIDER----")
Difficulty:
© Zi gZag Education, 2014
1
Python Challenge 5
Challenge 5: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
2
Python Challenge 6
Challenge 6: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
2
Python Challenge 7
Challenge 7: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
2
Python Challenge 8
Challenge 8: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
2
Python Challenge 9
Challenge 9: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
2
Python Challenge 10
Challenge 10: Solution
#!/usr/bin/python3
print ("----ROCK, PAPER, SCISSORS----")
import random
#generates a random number between 1 and 3
computer=random.randint(1,3)
#asks the user to input their choice and stores it in a variable
user=int(input("Enter your
choice:\n1=Rock\n2=Paper\n3=Scissors\n"))
#outputs the computer's move
print("The computer has chosen",computer)
#outputs a different string depending on the game outcome
if computer==user:
print ("Tie game")
elif computer==1 and user==3:
print ("Computer wins")
elif computer==2 and user==1:
print ("Computer wins")
elif computer==3 and user==2:
print ("Computer wins")
else:
print ("You Win!")
© Zi gZag Education, 2014
Python Challenge 11
Challenge 11
You will need to use:
Write a program that:
• asks the user to input a sentence Variables
• calculates and outputs how many
characters there are in the sentence String Manipulation
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 11
Challenge 11: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 12
Challenge 12: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 13
Challenge 13: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 14
Challenge 14: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 15
Challenge 15: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 16
Challenge 16: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 17
Challenge 17: Solution
#!/usr/bin/python3
num=1
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 18
Challenge 18: Solution
#!/usr/bin/python3
Tip: You must use a For Loop and a While Loop for this challenge.
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 19
Challenge 19: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 20
Challenge 20: Solution
#!/usr/bin/python3
guess=0
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 21
Challenge 21: Solution
#!/usr/bin/python3
print ("----INCHES/CM CONVERTER----")
#Converts inches to cm
def intocm(n):
convert=n*2.54
print(n,"inches =",convert,"cm")
return
#Converts cm to inches
def cmtoin(n):
convert=n*0.393700787
print(n,"cm =",convert,"inches")
return
#Asks the user to input a number and select the type of
conversion they want to perform
num=int(input("Enter the number you want to convert: "))
unit=int(input("Choose an option:\n1=INCHES to
CENTIMETRES\n2=CENTIMETRES to INCHES\n"))
#If statement calls the appropriate function
if unit==1:
intocm(num)
elif unit==2:
cmtoin(num)
© Zi gZag Education, 2014
Python Challenge 22
Challenge 22
You will need to use:
Write a program that:
• asks the user for the distance (in metres) Variables
• asks the user for the time in seconds
that a journey was completed in
• calculates and outputs the average
Selection
speed using a function
Functions
Difficulty:
© Zi gZag Education, 2014
3
Python Challenge 22
Challenge 22: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
4
Python Challenge 23
Challenge 23: Solution
#!/usr/bin/python3
import math
print ("----TURF CALCULATOR----")
#Function to calculate the amount of turf required
def calculate(w,l,r):
lawnArea=w*l
bedArea=math.pi*r*r
turf=lawnArea-bedArea
print("You need",turf,"square metres of turf")
return
#User input of width and length of the lawn and the
radius of the bed
width=int(input("Enter the width of the lawn: "))
length=int(input("Enter the length of the lawn: "))
radius=int(input("Enter the radius of the flower bed: "))
#Calls the calculate function, passing the width,
length and radius variables
calculate(width,length,radius)
Difficulty:
© Zi gZag Education, 2014
4
Python Challenge 24
Challenge 24: Solution
#!/usr/bin/python3
#Function to draw a line of the image
def drawLine(s,x):
for i in range(0,s):
print(" ", end=""),
for i in range(0,x):
print("X", end=""),
print()
return
#Function to call the drawLine function to draw image
def drawPicture():
drawLine(5,1)
drawLine(4,3)
drawLine(3,5)
drawLine(2,7)
drawLine(1,9)
drawLine(4,3)
drawLine(4,3)
return
drawPicture()
© Zi gZag Education, 2014
Python Challenge 25
Challenge 25
You will need to use:
Write a sign-up program for an after-
school club; it should ask the user for the Variables
following details and store them in a file:
• First Name
Selection
• Last Name
• Gender File Handling
• Form
Difficulty:
© Zi gZag Education, 2014
4
Python Challenge 25
Challenge 25: Solution
#!/usr/bin/python3
Difficulty:
© Zi gZag Education, 2014
4
Python Challenge 26
Challenge 26: Solution
#!/usr/bin/python3
def saveScore(n,s):
#Opens the file or creates it if it doesn't
already exist
file = open("scores.txt", "a")
#Records the user's score in the file
file.write("Name: "+n+", Score: "+str(s)+"\n")
#Closes the file
file.close()
return
print("----MATHS QUIZ----")
#Variable setup
name=input("Enter your name: ")
score=0
#Question 1
answer=int(input("What is 3 x 5?: "))
if answer==15:
score=score+1
...
© Zi gZag Education, 2014
Python Challenge 26
Challenge 26: Solution
...
#Question 2
answer=int(input("What is 10 + 13?: "))
if answer==23:
score=score+1
#Question 3
answer=int(input("What is 10 / 2?: "))
if answer==5:
score=score+1
Difficulty:
© Zi gZag Education, 2014
5
Python Challenge 27
Challenge 27: Solution
#!/usr/bin/python3
def saveScore(n,s):
#Opens the file or creates it if it doesn't already exist
file = open("scores.txt", "a")
#Records the user's score in the file
file.write("Name: "+n+", Score: "+str(s)+"\n")
#Closes the file
file.close()
return
print("----MATHS QUIZ----\n")
print("----SCORES----")
#Creates the scores.txt file if it doesn't exist
file = open("scores.txt", "a")
file.close()
#Opens the file in read-only mode
file = open("scores.txt", "r")
#Loop that prints each line from the file
for line in file:
print(line)
...
Difficulty:
© Zi gZag Education, 2014
5
Python Challenge 28
Challenge 28: Solution
#!/usr/bin/python3
import random
print("----RANDOM NAME GENERATOR----")
Difficulty:
© Zi gZag Education, 2014
5
Python Challenge 29
Challenge 29: Solution
#!/usr/bin/python3
print("----HOLIDAY CHECKLIST----")
#Creates the empty arrays
packList=[]
tasks=[]
#Asks the user to input the holiday info
name=input("Enter the destination of the holiday: ")
itemsNum=int(input("Enter the number of items you
need to pack: "))
tasksNum=int(input("How many tasks do you need to
complete to prepare for the holiday?: "))
#Loop to store the packing list
for i in range(0,itemsNum):
packList.append(input("Enter the name of item
"+str(i+1)+": "))
...
Difficulty:
© Zi gZag Education, 2014
5
Python Challenge 30
Challenge 30: Solution
#!/usr/bin/python3
#Function that can be used to ask each question
def ask(q,s):
answer=int(input(questions[0][q]))
if answer==questions[1][q]:
s=s+1
return s
print("----MATHS QUIZ----\n")
print("----SCORES----")
#Creates the scores.txt file if it doesn't exist
file = open("scores.txt", "a")
file.close()
#Opens the file in read-only mode
file = open("scores.txt", "r")
#Loop that prints each line from the file to the
screen
for line in file:
print(line)
file.close()
...
© Zi gZag Education, 2014
Python Challenge 30
Challenge 30: Solution
...
#Variable setup
name=input("Enter your name: ")
score=0
questions=[["What is 3 x 5?: ", "What is 10 + 13?:
", "What is 10 / 2?: "],[15,23,5]]
#Loop to call ask function to ask each question
for i in range(len(questions)+1):
score=ask(i,score)