Skip to content

Commit 88a8c76

Browse files
committed
basics exercises
1 parent c687cbb commit 88a8c76

16 files changed

+291
-0
lines changed

1 - Variables/basicsExercise1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Exercise 1 Basics
2+
# Write a program to input two messages and output them to a user
3+
4+
def main():
5+
message1= input("Enter your first message ")
6+
message2= input("Enter your second message ")
7+
print("Your first message was {0} ".format(message1))
8+
print("Your second message was {0} ".format(message2))
9+

1 - Variables/basicsExercise2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Exercise 2 Basics
2+
# Write a program to input two whole numbers,
3+
# add them together and print the result to the screen.
4+
5+
def main():
6+
number1= int(input("Enter your first number "))
7+
number2= int(input("Enter your second number "))
8+
sumOfNumbers = number1 + number2
9+
print("{0} + {1} = {2}".format(number1,number2,sumOfNumbers))
10+

2 - Selection/ifExercise1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Exercise 1 if statements
2+
# Write a function that asks the user how old they are.
3+
# Tell them if they are old enough to vote. Then extend the program
4+
# to tell them how many years it is until they can retire (assume at age 65).
5+
6+
def main():
7+
yourAge = int(input("How old are you? "))
8+
if yourAge >= 18:
9+
print("You are old enough to vote")
10+
else:
11+
ageToVote = 18- yourAge
12+
print("You can vote in {0} years ".format(ageToVote))
13+
14+
def extension():
15+
yourAge = int(input("How old are you? "))
16+
if yourAge >= 18:
17+
print("You are old enough to vote")
18+
else:
19+
ageToVote = 18- yourAge
20+
print("You can vote in {0} years ".format(ageToVote))
21+
if yourAge < 65:
22+
ageToRetire = 65- yourAge
23+
print("You can retire in {0} years.".format(ageToRetire))
24+
25+

2 - Selection/ifExercise2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Exercise 2 if statements
2+
# Write a function that asks the user to input a number
3+
# between 1 and 20. Give a response which indicates if the
4+
# number is either within the range, too high or too low.
5+
6+
7+
def main():
8+
number = int(input("Enter a number between 1 and 20 "))
9+
if number > 20:
10+
print("Your number is too high")
11+
elif number < 1:
12+
print("Your number is too low")
13+
else:
14+
print("Thank you!")
15+

2 - Selection/ifExercise3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Football scoring program
2+
3+
4+
def football():
5+
#Write a program that asks for two football team names, and then the score for each team
6+
#You program should then say if the teams get 3 points, 1 point or 0 points for the result
7+
print("This program will tell you how many points each football team scored based on the result")
8+
teamOne = input("Please enter the name of team one: ")
9+
teamTwo = input("Please enter the name of team two: ")
10+
teamOneScore = input("Please enter the score for {0}: ".format(teamOne))
11+
teamTwoScore = input("Please enter the score for {0}: ".format(teamTwo))
12+
13+
14+
15+
if teamOneScore > teamTwoScore:
16+
print("{0} score 3 points and {1} score 0 points".format(teamOne,teamTwo))
17+
elif teamOneScore < teamTwoScore:
18+
print("{0} score 3 points and {1} score 0 points".format(teamTwo,teamOne))
19+
else: #Game must be a draw if the previous two if statements are false
20+
print("Both {0} and {1} score 1 points.".format(teamOne,teamTwo))

3 - Iteration/farming.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The total amount of land used for farming in the UK is,a. 25%,b. 50%,c. 75%,c
2+
Which of these is not true? Wheat is used to make,a. plastic,b. bread,c. carpets,c
3+
The amount of European Union money spent on agricultural projects to improve the environment is,a. £50 million,b £150 million,c £300 million,b
4+
Which of these crops is not grown by farmers in the UK?,a. Hemp,b. Maize,c. Rice,c
5+
Which of the following is false?,a. The Environment Agency helps prevent flooding,b. The Environment Agency looks after food standards,c. The Environment Agency encourages care of the environment,b
6+
In a supermarket a pint of milk costs about 28p. The farmer gets paid,a. 9p,b. 17p,c. 21p,a
7+
Silage is,a. the manure/waste from cows,b. stored grass,c. a rock band with attitude,b
8+
The number of trees that farmers have planted in the last ten years is approximately,a. 30 million,b. 60 million,c. 90 million,c
9+
Margarine comes from,a. Cows,b. Oilseed crops,c. Barley,b
10+
Which of the following is not farmed in the UK?,a. worms,b. snails,c. ground beetles,c

3 - Iteration/forExercise1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For loop example
2+
3+
# Write a program that will ask the user for a message
4+
# and the number of times they
5+
# want that message displayed. Then output the message that number of times.
6+
7+
def messageLoop():
8+
message = input("Enter your message ")
9+
numberOfTimes = int(input("How many times do you want to see this message? "))
10+
for count in range(numberOfTimes):
11+
print(message)
12+

3 - Iteration/forExercise2.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# For loop example
2+
3+
# Write a program that will calculate the average (mean)
4+
##of a set of numbers. This time, the user is to be asked
5+
##how many numbers are to be averaged, they must then enter
6+
##this number of numbers.
7+
##Your program will calculate and display the average of those numbers.
8+
9+
def averaging():
10+
numberOfnumbers = int(input("How many numbers do you want to be averaged?"))
11+
runningTotal = 0
12+
# don't forget to initialise the running total before you start
13+
for count in range(numberOfnumbers):
14+
nextNumber = int(input("Enter the next number "))
15+
runningTotal = runningTotal + nextNumber
16+
average = runningTotal/numberOfnumbers
17+
print("The average of your numbers is {0}.".format(average))
18+
19+

4 - Lists/listsExercise1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ListsExercises1.py
2+
3+
def largest():
4+
maxNumber = 0
5+
numberList = [15,4,26,1,9,21,3,6,13]
6+
for each in numberList:
7+
if each>maxNumber:
8+
maxNumber = each
9+
print("The largest number in the list is {0}".format(maxNumber))
10+

4 - Lists/listsExercise2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ListsExercises2.py
2+
3+
def wordsBegS():
4+
wordList = ["and","but","sausage","every","fish","sugar","pie","sultry"]
5+
numberSwords = 0
6+
7+
for each in wordList:
8+
if each[0]=="s":
9+
print(each)
10+
numberSwords = numberSwords+1
11+
12+
print("End of program")
13+

0 commit comments

Comments
 (0)