8000 Merge branch 'master' of https://github.com/ihf-code/python_twitch · zfoxpython/intro-to-python@a000333 · GitHub
[go: up one dir, main page]

Skip to content

Commit a000333

Browse files
committed
2 parents c629906 + 7651ab7 commit a000333

File tree

14 files changed

+227
-33
lines changed

14 files changed

+227
-33
lines changed

session_03/answers/A1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# A1 - Ask for the user's name, if they are called "Bob", print "Welcome Bob!"
22

3+
# Created variable name and assigned the input to it.
4+
# Whatever the user enters as their name will be stored in the name variable.
35
name = input("What is your name?\n")
46

7+
# Checking if the name the user has input is Bob.
8+
# If it is, print "Welcome Bob!"
59
if name == "Bob":
610
print("Welcome Bob!")

session_03/answers/A2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# A2 - Ask for the user's name, if they are not called "Alice", print "You're not Alice!"
22

3+
# Created variable name and assigned the input to it.
4+
# Whatever the user enters as their name will be stored in the name variable.
35
name = input("What is your name?\n")
46

7+
# Checking if the name the user has input is not Alice.
8+
# If it is not Alice, print "You're not Alice!"
59
if name != "Alice":
610
print("You're not Alice!")

session_03/answers/A3.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# A3 - Ask the user for a password, if they enter the password "qwerty123", print "You have successfully logged in".
22
# If they get it wrong, print "Password failure"
33

4-
password = "qwerty123"
4+
# Created variable name and assigned the input to it.
5+
# Whatever the user enters as their password will be stored in the user_guess variable.
56
user_guess = input("What is your password?\n")
7+
# Hardcoded password variable so we can compare this against the user_guess
8+
password = "qwerty123"
69

10+
# Checking if the password is the same as the user_guess.
11+
# If it is the same, print "You have successfully logged in."
712
if user_guess == password:
813
print("You have successfully logged in.")
14+
# If it is not the same, print "Password failure."
915
else:
1016
print("Password failure.")

session_03/answers/A4.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# A4 - Ask the user to enter a number, if the number is even, print "Even", otherwise print "Odd"
22

3+
# Created variable number and assigned the input to it.
4+
# As we will be performing some math on this input, we will need to cast it to an integer.
5+
# Whatever the user enters as their number will be stored in the number variable.
36
number = int(input("Please enter a number to find out if it is even or odd:\n"))
47

8+
# Checking if the number is even, we do this by using the modulus to check if when divided by 2 the remainder is 0
9+
# If it is the same, print(str(number) + " is Even") - as number is an integer, to concatenate, we have to cast to a string.
510
if number % 2 == 0:
611
print(str(number) + " is Even")
12+
# If it is not even, it must be odd so will print(str(number) + " is Odd"), again casting the number to a string.
713
else:
814
print(str(number) + " is Odd")

session_03/answers/A5.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# A5 - Ask the user for 2 different numbers, if the total of the two numbers is over 21, print "Bust" otherwise print "Safe"
22

3+
# Created variable number and assigned the input to it.
4+
# As we will be performing some math on this input, we will need to cast it to an integer.
5+
# Whatever the user enters as their number will be stored in the number_1 variable.
6+
# Repeated the above for number_2
37
number_1 = int(input("Input a number:\n"))
48
number_2 = int(input("Input another number:\n"))
59

10+
# Checking if when both number variables are added together are over 21, print("Bust.")
611
if number_1 + number_2 > 21:
712
print("Bust.")
13+
# if when both number variables are added together are under 21, print("Safe.")
814
else:
915
print("Safe.")

session_04/answers/B10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# D5 - FizzBuzz – Write a program that prints the numbers from 1 to 100.
1+
# B10 - FizzBuzz – Write a program that prints the numbers from 1 to 100.
22
# For multiples of three, print “Fizz” instead of the number and for multiples of five, print “Buzz”.
33
# For numbers which are multiples of both three and five, print “FizzBuzz”
44

session_05/answers/A5.py renamed to session_05/answers/A11.py

Lines changed: 1 addition & 1 deletion
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# A5 - Rock, Paper, Scissors - Create a simple rock, paper, scissors game which is run against computer.
1+
# A11 - Rock, Paper, Scissors - Create a simple rock, paper, scissors game which is run against computer.
22

33
import random
44

session_06/slides/session_6.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ while times_in_loop <= 10:
197197
x = 1
198198
while x <= 100:
199199
print(x)
200-
times_in_loop = times_in_loop + 1
200+
x = x + 1
201201

202202
for y in range(1, 101):
203203
print(y)

session_08/answers/A2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# A2 - Read the file 'austen.txt' and print the amount of lines in the file
2+
3+
#Option 1
24
total = 0
35
for x in open("austen.txt"):
46
total += 1
7+
58
print(total)
9+
10+
#Option 2
11+
f = open("austen.txt", "r")
12+
count = 0
13+
for x in f:
14+
count += 1
15+
16+
print(count)

session_08/answers/A3.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# A3 - Each line of the file 'numbers.txt' contains a number, write a script to add up all the values in the file
2+
3+
#Option 1
24
total = 0
35
for x in open("numbers.txt"):
46
total += int(x)
7+
58
print(total)
9+
10+
#Option 2
11+
f = open("numbers.txt", "r")
12+
sum = 0
13+
for x in f:
14+
x = int(x)
15+
sum += x
16+
17+
print(sum)

session_08/answers/B1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# B1 - Ask the user to enter their name and append this to a file called 'register.txt'
22

33
file1 = open("register.txt", "a")
4+
45
name = True
56
while name != "":
6-
name = input("whats uyour anme? ")
7+
name = input("What's your name?\n")
78
if name:
89
file1.write(name + "\n")
910

session_08/answers/B2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# B2 - Create a new file called 'even.txt' that contains only the even numbers from the file 'numbers.txt"
22

33
f = open("even.txt", "w")
4+
45
for x in open("numbers.txt"):
56
x = int(x)
67
if x % 2 == 0:
78
f.write(str(x) + "\n")
9+
10+
f.close()

session_08/answers/B4.py

Lines changed: 160 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,164 @@
33
# Fake data is usually evenly distributed, where as real data The files 'accounts_1.txt', 'accounts_2.txt' and 'accounts_3.txt' contain financial transaction data.
44
# Work out which of the files contains fake data.
55

6+
# STAGE 1 - this code allows us to check the values of one of the account files.
7+
f = open("accounts_1.txt", "r")
8+
9+
count = {
10+
"0":0,
11+
"1":0,
12+
"2":0,
13+
"3":0,
14+
"4":0,
15+
"5":0,
16+
"6":0,
17+
"7":0,
18+
"8":0,
19+
"9":0
20+
}
21+
22+
for x in f:
23+
if x:
24+
count[x[0]] += 1
25+
26+
for y in range(1,10):
27+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
28+
29+
# STAGE 2 - Adding a for loop so we are able to loop through all accounts.
30+
for x in range(1,4):
31+
f = open("accounts_" + str(x) + ".txt", "r")
32+
33+
count = {
34+
"0":0,
35+
"1":0,
36+
"2":0,
37+
"3":0,
38+
"4":0,
39+
"5":0,
40+
"6":0,
41+
"7":0,
42+
"8":0,
43+
"9":0
44+
}
45+
46+
for num in f:
47+
if num:
48+
count[num[0]] += 1
49+
print(x)
50+
for y in range(1,10):
51+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
52+
53+
#STAGE 3 - Changing count so instead of us manually having to input count, it will create itself
654
for x in range(1,4):
7-
f = open("accounts_" + str(x) + ".txt", "r")
8-
count = {
9-
"0": 0,
10-
"1": 0,
11-
"2": 0,
12-
"3": 0,
13-
"4": 0,
14-
"5": 0,
15-
"6": 0,
16-
"7": 0,
17-
"8": 0,
18-
"9": 0,
19-
}
20-
21-
for num in f:
22-
if num:
23-
count[num[0]] += 1
24-
25-
print(x)
26-
for y in range(1,10):
27-
print(str(y) + " = " + str(count[str(y)]/100) + "%")
55+
f = open("accounts_" + str(x) + ".txt", "r")
56+
57+
count = {}
58+
for i in range(1,10):
59+
count[str(i)] = 0
60+
61+
for num in f:
62+
if num:
63+
count[num[0]] += 1
64+
print(x)
65+
for y in range(1,10):
66+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
67+
68+
# STAGE 4 - Putting our code into a function - call 3 times
69+
70+
def benford_calc(file_name):
71+
f = open(file_name, "r")
72+
73+
count = {}
74+
for i in range(1,10):
75+
count[str(i)] = 0
76+
77+
for num in f:
78+
if num:
79+
count[num[0]] += 1
80+
print("Results for:" + file_name)
81+
for y in range(1,10):
82+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
83+
84+
benford_calc("accounts_1.txt")
85+
benford_calc("accounts_2.txt")
86+
benford_calc("accounts_3.txt")
87+
88+
# STAGE 5 - Looping through file names, so we don't need to call the function 3 times, as per stage 4
89+
def benford_calc(file_name):
90+
f = open(file_name, "r")
91+
92+
count = {}
93+
for i in range(1,10):
94+
count[str(i)] = 0
95+
96+
for num in f:
97+
if num:
98+
count[num[0]] += 1
99+
print("Results for:" + file_name)
100+
for y in range(1,10):
101+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
102+
103+
for x in range (1,4):
104+
file_name = "accounts_" + str(x) + ".txt"
105+
benford_calc(file_name)
106+
107+
# STAGE 6 - Separating file function from data function
108+
109+
def benford_calc_file(file_name):
110+
f = open(file_name, "r")
111+
return benford_calc(f)
112+
113+
def benford_calc(data):
114+
115+
count = {}
116+
for x in range(1,10):
117+
count[str(x)] = 0
118+
119+
for num in data:
120+
if num:
121+
count[str(num[0])] += 1
122+
123+
return count
124+
125+
for x in range (1,4):
126+
file_name = "accounts_" + str(x) + ".txt"
127+
data = benford_calc_file(file_name)
128+
129+
print("Results for:" + file_name)
130+
131+
for y in range(1,10):
132+
print(str(y) + " = " + str(data[str(y)]/100) + "%")
133+
134+
# STAGE 7 - Sending in a list of random numbers
135+
136+
def benford_calc_file(file_name):
137+
f = open(file_name, "r")
138+
return benford_calc(f)
139+
140+
def benford_calc(data):
141+
142+
count = {}
143+
for x in range(1,10):
144+
count[str(x)] = 0
145+
146+
for num in data:
147+
if num:
148+
count[str(num[0])] += 1
149+
150+
return count
151+
152+
for x in range (1,4):
153+
file_name = "accounts_" + str(x) + ".txt"
154+
data = benford_calc_file(file_name)
155+
156+
print("Results for:" + file_name)
157+
158+
for y in range(1,10):
159+
print(str(y) + " = " + str(data[str(y)]/100) + "%")
160+
161+
random_nums = [str(random.randint(1,100)) for x in range(1,10001)]
162+
data = benford_calc(random_nums)
163+
print("Results for Random Nums")
164+
165+
for y in range(1,10):
166+
print(str(y) + " = " + str(data[str(y)]/100) + "%")

session_10/exercises/exercises_10.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
## Section 10 Exercises
22

33
# Section A
4-
1. Create a Person class, initialise it with a name. Create a method for the Person class that will say hello to the name.
5-
2. Create a Circle class, initialise it with a radius. Create two methods for the Circle class: get_area() and get_circumference() which give both respective areas and circumference, to 2 decimal placese.
4+
1. Create a class called Upper which has two methods called get_word and print_word.
5+
- the get_word method should accept a string from the user
6+
- the print_word method prints the string in upper case.
7+
2. Create a Person class, initialise it with a name. Create a method for the Person class that will say hello to the name.
8+
3. Create a Circle class, initialise it with a radius. Create two methods for the Circle class: get_area() and get_circumference() which give both respective areas and circumference, to 2 decimal placese.
69
- Note: Area of a circle = πr ** 2
710
- Circumference = 2πr
811
- Use the round() function to get the answer to 2 decimal places
9-
3. Create a Employee class and initialise it with name and staff number.
12+
4. Create a Employee class and initialise it with name and staff number.
1013
- i. Make methods to:
1114
- display_info - It should display all the information of the employee.
1215
- set_department - It should assign the department to employee.
@@ -17,13 +20,12 @@
1720
- Given a person's first and last names:
1821
- Form the full_name method by simply joining the first and last name together, separated by a space.
1922
- Form the email_address by joining the first and last name together with a . in between, and follow it with @company.com at the end. Make sure everything is in lowercase.
20-
4. Create a Vehicle parent class, initialise it with, wheels, colour and a method to display all this information.
23+
24+
5. Create a Vehicle parent class, initialise it with, wheels, colour and a method to display all this information.
2125
- i. Create a Tesla (or any car) child classs and add a method to get the miles and a method to display all this information.
2226
- ii. Change the colour of the vehicle.
2327
- iii. Delete the wheels.
24-
5. Create a class called Upper which has two methods called get_word and print_word.
25-
- the get_word method should accept a string from the user
26-
- the print_word method prints the string in upper case.
28+
2729
6. Create a Sandwich class with the attributes order_number and ingredients.
2830
- i. The ingredients attributes is given as a list - (Note: use list(<atrribute>) to enable this).
2931
- Only the ingredients attributes will be given as input.

0 commit comments

Comments
 (0)
0