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

+9
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

+10
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

+25
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

+15
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

+20
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

+10
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

+12
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

+19
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

+10
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

+13
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+

4 - Lists/listsExercise3.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# ListsExercise3.py
2+
3+
# Write a function which finds the averages of a list of number using a for loop
4+
5+
def averageProg():
6+
runningTotal = 0
7+
listOfNumbers = [4,7,9,1,8,6]
8+
for each in listOfNumbers:
9+
runningTotal = runningTotal + each
10+
# each time round the loop add the next item to the running total
11+
average = runningTotal/len(listOfNumbers)
12+
# the average is the runningTotal at the end / how many numbers
13+
print(listOfNumbers)
14+
print("The average of these numbers is {0:.2f}".format(average))
15+
16+

4 - Lists/listsExercise4.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## ListsExercise4.py
2+
3+
4+
5+
def farmList():
6+
print("Please enter six farming terms")
7+
termList = []
8+
for counter in range(6):
9+
term = input("Enter the next term ")
10+
termList.append(term)
11+
print(termList)
12+
13+
def farmList2():
14+
print("Please enter six farming terms")
15+
termList = []
16+
for counter in range(6):
17+
term = input("Enter the next term ")
18+
termList.append(term)
19+
reverseOrder = input("Do you want it printed out in reverse? (y/n)")
20+
if reverseOrder == "y":
21+
print(termList[::-1])
22+
else:
23+
print(termList)
24+
25+
26+
def farmList3():
27+
print("Please enter six farming terms")
28+
termList = []
29+
for counter in range(6):
30+
term = input("Enter the next term ")
31+
termList.append(term)
32+
reverseOrder = input("Do you want it printed out in reverse? (y/n)")
33+
if reverseOrder == "y":
34+
print(termList[::-1])
35+
else:
36+
print(termList)
37+
whichTerm = int(input("Which term (1-6) would you like to see again? "))
38+
print(termList[whichTerm-1]) # minus 1 as our list starts at 0

4 - Lists/listsExercise5.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# listsExercise5.py
2+
3+
def diceThrowing():
4+
# tallychart program
5+
import random
6+
tally = [0,0,0,0,0,0]
7+
for counter in range(30):
8+
score = random.randint(1, 6)
9+
tally[score-1] = tally[score-1] + 1 #update tally chart
10+
print("|{0:<8} | {1:<10}|".format("Number","Frequency"))
11+
12+
for counter in range(1,7):
13+
print("|{0:^8} | {1:^10}|".format(counter,tally[counter-1]))
14+
15+

5 - Functions/FunctionsExercise1.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# FunctionsExercise1.py
2+
3+
def inputData():
4+
number = int(input("Enter a number "))
5+
return number
6+
7+
def processData(number):
8+
if number % 7 == 0:
9+
# divisible by 7
10+
return True
11+
else:
12+
return False
13+
14+
def outputData(number,result):
15+
if result:
16+
print("The number {0} is divisible by 7".format(number))
17+
else:
18+
print("The number {0} is not divisible by 7".format(number))
19+
20+
def main():
21+
userNumber = inputData()
22+
result = processData(userNumber)
23+
outputData(userNumber,result)
24+
25+

5 - Functions/FunctionsMenu.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# FunctionsMenu.py
2+
#menu program
3+
4+
5+
def mainmenu ():
6+
choice = 0
7+
while choice != "4":
8+
9+
print()
10+
print()
11+
print("Choose from this menu")
12+
print()
13+
print("1 - Maze Game")
14+
print("2 - Guessing Game")
15+
print("3 - Quiz")
16+
print("4 - Exit")
17+
print ()
18+
choice = input("Enter 1,2,3 or 4")
19+
20+
if choice == "1":
21+
mazeGame()
22+
elif choice == "2":
23+
numberGuesser()
24+
elif choice == "3":
25+
quiz()
26+
elif choice == "4":
27+
print ("Thanks for using the program.")
28+
else:
29+
print("Please enter 1,2,3 or 4 only")
30+
31+
32+
def mazeGame():
33+
print("Now running Maze game...")
34+
def numberGuesser():
35+
print("Now running Guessing game")
36+
def quiz():
37+
print("Now running quiz")
38+
39+
40+
41+
42+
43+
44+

6 - Files/farming.csv

+10
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

0 commit comments

Comments
 (0)