10000 Updated session 5 slides/answers and multiple choice questions · nrowbotham/intro-to-python@f354faf · GitHub
[go: up one dir, main page]

Skip to content

Commit f354faf

Browse files
committed
Updated session 5 slides/answers and multiple choice questions
1 parent 53c0e1b commit f354faf

File tree

10 files changed

+172
-105
lines changed

10 files changed

+172
-105
lines changed

session_05/answers/A10.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# A10 - Ask the user to enter the names of people who entered a prize draw. Select a random winner and print their name
2+
3+
import random
4+
prize_draw_list = []
5+
user_input = None
6+
7+
# while the user does not enter a blank space, keep asking for names to be added to the draw
8+
while user_input != "":
9+
user_input = input("Please input your name to be added to the prize draw:\n")
10+
# only if the user enters a name and not an empty string will it be added to the prize draw list
11+
if user_input != "":
12+
prize_draw_list.append(user_input)
13+
14+
# getting a random item out of the list and printing it as the winner
15+
print("Congratulations! " + random.choice(prize_draw_list) + " you are the winner of the prize draw!")

session_05/answers/A11.py

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,48 @@
22

33
import random
44

5-
print("\nWelcome to Rock, Paper, Scissors.\n You vs the Computer.\n Best of Three Wins\n")
6-
computer_score = 0
5+
print("Welcome to Rock, Paper, Scissors")
6+
77
user_score = 0
8-
while computer_score < 3 and user_score < 3:
9-
computer_choice = random.choice(["rock", "paper", "scissors"])
10-
print(computer_choice)
11-
print(user_score)
12-
user_choice = input("Make your choice - rock, paper or scissors:\n")
13-
if user_choice == computer_choice:
14-
print("Draw")
15-
computer_score += 1
16-
user_score += 1
17-
elif user_choice == "rock" and computer_choice == "paper":
18-
print("You lose")
19-
computer_score += 1
20-
elif user_choice == "scissors" and computer_choice == "rock":
21-
print("You lose")
22-
computer_score += 1
23-
elif user_choice == "paper" and computer_choice == "scissors":
24-
print("You lose")
25-
computer_score += 1
26-
elif user_choice == "rock" and computer_choice == "scissors":
27-
print("You win")
28-
user_score += 1
29-
elif user_choice == "scissors" and computer_choice == "paper":
30-
print("You win")
31-
user_score += 1
32-
elif user_choice == "paper" and computer_choice == "rock":
33-
print("You win")
34-
user_score += 1
35-
else:
36-
print("Please choose rock, paper or scissors")
37-
continue
38-
print("Game Over")
8+
computer_score = 0
9+
turns = 0
10+
11+
while turns < 3:
12+
user_choice = input("What is your move? (rock, paper, scissors) ")
13+
computer_choice = random.choice(["rock", "paper", "scissors"])
14+
print("You picked " + user_choice)
15+
print("The computer picked " + computer_choice)
16+
turns += 1
17+
print("This is turn: " + str(turns))
18+
if user_choice == "rock":
19+
if computer_choice == "scissors":
20+
print("You Win")
21+
user_score += 1
22+
elif computer_choice == "paper":
23+
print("You Lose")
24+
computer_score += 1
25+
else:
26+
print("It's a draw")
27+
elif user_choice == "paper":
28+
if computer_choice == "rock":
29+
print("You Win")
30+
user_score += 1
31+
elif computer_choice == "scissors":
32+
print("You Lose")
33+
computer_score += 1
34+
else:
35+
print("It's a draw")
36+
else:
37+
if computer_choice == "paper":
38+
print("You Win")
39+
user_score += 1
40+
elif computer_choice == "rock":
41+
print("You Lose")
42+
computer_score += 1
43+
else:
44+
print("It's a draw")
45+
46+
print("Game Over! Final Score: User Score: " + str(user_score) + "\n Computer Score: " + str(computer_score))
47+
48+
49+

session_05/answers/A2.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# A2 - i. Keep asking the user to enter a number until they enter the number 7, then print "Wow! Lucky number 7!
2-
# A2 - ii. Rewrite so that the number being guessed is a random value from 1 to 10
2+
# A2 - ii. Rewrite so that the number being guessed is a random value from 1 to 10, instead of number 7
33

44
# i
55
# guess variable stored with a None value, this means it has no value.
@@ -12,19 +12,12 @@
1212
print("Wow! Lucky number 7!")
1313

1414
# ii
15-
# as above, but changed guess to a random number between 1 - 10 instead of the user inputting a number.
16-
guess = None
17-
while guess != 7:
18-
guess = random.randint(1, 10)
19-
print(guess)
20-
print("Wow! Lucky number 7!")
21-
22-
# iii
2315
# this has now been refactored to be a guessing game between the user and computer.
2416
# the computer picks a random number between 1-10, the user has to try and guess the answer.
2517
# it will keep asking until the user matches.
18+
import random
2619
computer_choice = random.randint(1, 10)
27-
user_choice = 0
20+
user_choice = None
2821
while computer_choice != user_choice:
2922
user_choice = int(input("Guess the computer's number:\n"))
3023
print("Well done! You guess the computer's number!")

session_05/answers/A4.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@
2121
print("PASSWORD FAILURE\n")
2222
user_input = input("What is your password?\n")
2323
user_guesses += 1
24-
print("System Failure")
24+
25+
if user_guesses == 2:
26+
print("PASSWORD FAILURE\n")
27+
print("System Failure - You have entered 3 incorrect passwords.")

session_05/examples/.gitignore

Whitespace-only changes.

session_05/exercises/exercises_5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## Section A
33
1. Print 10 random numbers
44
2. Keep asking the user to enter a number until they enter the number 7, then print "Wow lucky number 7!"
5-
- Rewrite so that the number being guessed is a random value from 1 to 10
5+
- Rewrite so that the number being guessed is a random value from 1 to 10 instead of number 7
66
3. The area of a rectangle is width multiplied by height. Ask the user to enter a width and height in cm, then print the area to the whole square metre. E.g. 240cm x 80cm = 19200cm2 = 2m2
77
4. Ask the user for a password, if they enter the password "qwerty123", print "You have successfully logged in". If they get it wrong, print "Password failure" and then ask them t F438 o enter it again. Only allow them to enter the password wrong 3 times before printing "System Locked!"
88
5. Add up all the numbers from 1 to 500 and print the answer
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# KPMG:Code - Session 5 - Multiple Choice Questions
2+
3+
1. What is a module?
4+
- a library of code we can reuse in our code - A
5+
- a statement that allows a computer to make a decision
6+
- a way to print an output
7+
- a collection of data
8+
9+
2. What key word brings the module in so our code can use it?
10+
- bring
11+
- input
12+
- import - A
13+
- contain
14+
15+
3. What does the random.randint() function do?
16+
17+
- picks a random number from 0 to 1
18+
- picks a random number from a range you specify - A
19+
- picks a random item from a list
20+
- picks a random function
21+
22+
4. What does the random.choice() function do?
23+
24+
- picks a random number from 0 to 1
25+
- picks a random number from a range you specify
26+
- picks a random item from a list - A
27+
- picks a random function
28+
29+
5. What will be the output of the below:
30+
import math
31+
print(math.ceil(4.2))
32+
33+
- 4
34+
- 5 - A
35+
- 4.2
36+
- 5.2
37+
38+
6. What number will stop this while loop:
39+
guess = None
40+
while guess != 4:
41+
guess = int(input("What's your number? "))
42+
43+
- 0
44+
- 3
45+
- 4 - A
46+
- 7
47+
48+
7. How many times will "Hello" be printed?
49+
times_in_loop = 0
50+
51+
while times_in_loop >= 0:
52+
print("Hello")
53+
times_in_loop += 1
54+
if times_in_loop > 5:
55+
break
56+
57+
- 0
58+
- 4
59+
- 5
60+
- 6 - A
61+
62+
8. The below loop will not run forever:
63+
times_in_loop = 0
64+
65+
while times_in_loop >= 0:
66+
print("Hello")
67+
times_in_loop += 1
68+
69+
- True
70+
- False - A
71+
72+
9. To print 5 random numbers, fill in the blanks:
73+
import random
74+
number = 0
75+
76+
while number <= ?:
77+
print(random.?(1,10))
78+
number += 1
79+
80+
- 3, random
81+
- 4, randint
82+
- 5, choice
83+
- 4, random
84+
85+
10. What will be the output of the below:
86+
sum=0
87+
i=0
88+
89+
while i < 10:
90+
i += 1
91+
sum += i
92+
93+
print(sum)
94+
95+
- 10
96+
- 45
97+
- 55 - A
98+
- 100

session_05/slides/answers_5.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
# [fit] IHF: Code
1+
# [fit] KPMG: Code
22
## [fit] Python — Session 5 — Answers
3-
### Live at 10am
43

54
---
65

76
## Any Questions
8-
### go to sli 6851 .do #python2020
9-
10-
---
11-
12-
# [fit] Next Session
13-
### Tuesday 16th June 10am
14-
### Lesson 6

session_05/slides/session_5.md

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,11 @@
1-
# [fit] IHF: Code
1+
# [fit] KPMG: Code
22
## [fit] Python — Session 5 — Lesson
3-
### Live at 10am
43
---
54

65
# Review
76

87
---
98

10-
# If
11-
12-
```python
13-
age = int(input("How old are you? "))
14-
if age >= 18:
15-
print("You can vote!")
16-
```
17-
18-
---
19-
20-
# If / Else
21-
22-
```python
23-
age = int(input("How old are you? "))
24-
if age >= 18:
25-
print("You can vote!")
26-
else:
27-
print("Try voting next time")
28-
```
29-
30-
---
31-
32-
# If / Else / Elif
33-
34-
```python
35-
name = "Bob"
36-
if name == "Alice":
37-
print("Hello Alice")
38-
elif name == "Bob":
39-
print("Hello Bob")
40-
else:
41-
print("You must be Charlie")
42-
```
43-
44-
---
45-
469
# List
4710
```python
4811
names = ["Alice", "Bob", "Charlie"]
@@ -208,7 +171,6 @@ for x in range(times_to_ask):
208171
---
209172

210173
## Any Questions?
211-
### sli.do #python2020
212174

213175
---
214176

@@ -338,9 +300,9 @@ while times_in_loop <= 10:
338300
# While Loops
339301

340302
```python
341-
x = 1
342-
while x <= 100:
343-
print(x)
303+
times_in_loop = 1
304+
while times_in_loop <= 100:
305+
print(times_in_loop)
344306
times_in_loop = times_in_loop + 1
345307

346308
# ...
@@ -489,10 +451,3 @@ for x in range(times_to_loop):
489451
---
490452

491453
### Questions?
492-
### go to sli.do #python2020
493-
494-
---
495-
496-
# [fit] Next Session
497-
### Thursday 11th June 10am
498-
### Answers

session_05/slides/thumbnails_5.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# [fit] IHF: Code
1+
# [fit] KPMG: Code
22
## [fit] Python — Session 5 — Lesson
33

44
---
55

6-
# [fit] IHF: Code
6+
# [fit] KPMG: Code
7< 3593 /td>7
## [fit] Python — Session 5 — Answers

0 commit comments

Comments
 (0)
0