Skip to content

Commit f9b30ec

Browse files
committed
Merge branch 'refs/heads/Files_Quiz_consolidation_extension'
2 parents 2ef68f1 + e81e92e commit f9b30ec

14 files changed

+155
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with open("shopping.txt", mode="r",encoding="utf-8") as my_file:
2+
shopping_list = my_file.read().splitlines()
3+
print("My Shopping List")
4+
print()
5+
for count,item in enumerate(shopping_list):
6+
print("{0}. {1}".format(count+1,item))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def bubble_sort(unsorted):
2+
no_swaps = True
3+
while no_swaps:
4+
no_swaps = False
5+
for item in range(1,len(unsorted)-1):
6+
if unsorted[item] > unsorted[item+1]:
7+
temp = unsorted[item+1]
8+
unsorted[item+1] = unsorted[item]
9+
unsorted[item] = temp
10+
no_swaps = True
11+
12+
13+
with open("students_unsorted.txt",mode="r",encoding="utf-8") as my_file:
14+
students = my_file.read().splitlines()
15+
bubble_sort(students)
16+
with open("students_sorted.txt",mode="w",encoding="utf-8") as my_file:
17+
for student in students:
18+
my_file.write(student+'\n')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
users = []
2+
with open("usernames_passwords.txt", mode="r",encoding="utf-8") as my_file:
3+
for line in my_file:
4+
users.append(line.rstrip("\n").split(','))
5+
print(users)
6+
attempts = 3
7+
logged_in = False
8+
while attempts > 0 and not logged_in:
9+
username_found = False
10+
password_found = False
11+
username = input("Please enter your username: ")
12+
password = input("Please enter your password: ")
13+
for details in users:
14+
if username == details[0] and password == details[1]:
15+
logged_in = True
16+
elif username == details[0]:
17+
username_found = True
18+
if not logged_in and username_found:
19+
print("Incorrect password")
20+
elif not logged_in and not username_found:
21+
print("This username does not exist")
22+
attempts -= 1
23+
if logged_in:
24+
print("Login successful")
25+
else:
26+
print("You did not login succesfully after 3 attempts")
27+
28+
29+
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
names = []
2+
for count in range(6):
3+
name = input("Please enter a name: ")
4+
names.append(name)
5+
6+
with open("names.txt",mode="w",encoding="utf-8") as my_file:
7+
for name in names:
8+
my_file.write(name+"\n")

Files/Exercise Solutions/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

Files/Exercise Solutions/names.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Adam
2+
Beth
3+
Fraser
4+
Claire
5+
Brant
6+
Rachel

Files/Exercise Solutions/quiz.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import csv, random
2+
3+
def get_questions():
4+
questions = []
5+
with open("farming.txt",mode="r",encoding="utf-8") as my_file:
6+
reader = csv.reader(my_file)
7+
for row in reader:
8+
questions.append(row)
9+
return questions
10+
11+
def ask_question(question,score):
12+
print(question[0])
13+
for multi in question[1:-1]:
14+
print("{0:>5}{1}".format("",multi))
15+
answer = input("Please select an answer: ")
16+
print()
17+
if answer == question[-1]:
18+
print("Correct!")
19+
score += 1
20+
else:
21+
print("Incorrect! - the correct answer was {0}.".format(question[-1]))
22+
print()
23+
return score
24+
25+
def main():
26+
questions = get_questions()
27+
score = 0
28+
print("Welcome to the farming quiz!")
29+
print("============================")
30+
print()
31+
print()
32+
number = int(input("There are {0} questions - how many do you want in your quiz: ".format(len(questions))))
33+
while number > 0:
34+
question = random.choice(questions)
35+
score = ask_question(question, score)
36+
questions.remove(question)
37+
number -= 1
38+
print("Your final score was {0} out of {1}".format(score,len(questions)))
39+
40+
if __name__ == "__main__":
41+
main()

Files/Exercise Solutions/shopping.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Bread
2+
Milk
3+
Oranges
4+
Bananas
5+
Salad
6+
Tomatoes
7+
Biscuits
8+
Washing Power
9+
Bin Bags
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Fraser
2+
Beth
3+
Claire
4+
Emma
5+
Martin
6+
Matthew
7+
Neil
8+
Sarah
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Fraser
2+
Emma
3+
Beth
4+
Neil
5+
Sarah
6+
Matthew
7+
Martin
8+
Claire
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
12345,password
2+
34521,letmein
3+
90987,helloworld

Files/new_animals.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dog
2+
cat
3+
horse

Files/read_file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
with open("animals.txt", mode="r",encoding="utf-8") as my_file:
22
for line in my_file:
3-
print(line)
3+
print(line.rstrip('\n'))

Files/write_file.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
animals = ["dog","cat","horse"]
2+
with open("new_animals.txt", mode="w",encoding="utf-8") as my_file:
3+
for animal in animals:
4+
my_file.write(animal+"\n")

0 commit comments

Comments
 (0)