You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: session_05/exercises_5.py
+154-1Lines changed: 154 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -3,42 +3,122 @@
3
3
## Section A
4
4
# 1. Print 10 random numbers.
5
5
6
+
# import random
7
+
# x = 0
8
+
# while x < 9:
9
+
# print(random.randint(0, 100))
10
+
# x += 1
6
11
7
12
8
13
# 2. Keep asking the user to enter a number until they enter the number 7, then print "Wow lucky number 7!".
9
14
# - Rewrite so that the number being guessed is a random value from 1 to 10 instead of number 7 .
10
15
16
+
# number = None
11
17
18
+
# while number != 7:
19
+
# number = int(input("Enter a number: "))
20
+
21
+
# print("Wow, lucky number 7!")
12
22
13
23
# 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.
14
24
# E.g. 240cm x 80cm = 19200cm2 = 2m2.
25
+
# import math
26
+
# width = int(input("Enter a width in cm: "))
27
+
# length = int(input("Enter a height in cm: "))
28
+
# area_cm = width * length
29
+
# area_m = math.floor(area_cm/10000)
15
30
31
+
# print("The area is " + str(area_m) + "m^2")
16
32
17
33
18
34
# 4. Ask the user for a password, if they enter the password "qwerty123", print "You have successfully logged in".
19
35
# If they get it wrong, print "Password failure" and then ask them to enter it again.
20
36
# Only allow them to enter the password wrong 3 times before printing "System Locked!".
21
37
22
-
38
+
# password = None
39
+
# attempts = 0
40
+
41
+
# while attempts < 3:
42
+
# if password != "qwerty123":
43
+
# password = input("Please enter your password: ")
44
+
# print("Password failure")
45
+
# attempts += 1
46
+
# else:
47
+
# print("You have successfully logged in")
48
+
# break
49
+
50
+
51
+
# if attempts == 3:
52
+
# print("You have entered your password incorrectly 3 times.")
53
+
# print("System Locked!")
54
+
55
+
56
+
# password = input("Please enter your password: ")
57
+
# correct_password = "qwerty123"
58
+
# attempts = 0
59
+
60
+
# while attempts <2:
61
+
# if password == correct_password:
62
+
# print("You have successfully logged in.")
63
+
# break
64
+
# else:
65
+
# print("Password failure")
66
+
# attempts += 1
67
+
# password = input("Please enter your password: ")
68
+
69
+
# if attempts == 2:
70
+
# print("System failure - you have entered your password incorrectly 3 times.")
71
+
23
72
24
73
# 5. Add up all the numbers from 1 to 500 and print the answer.
25
74
75
+
# number = 0
76
+
# sum = 0
77
+
78
+
# while number < 501:
79
+
# sum = sum + number
80
+
# number += 1
81
+
82
+
# print(sum)
26
83
27
84
28
85
# 6. Create a blank list. Ask the user to input 10 numbers (one should be the number 99) into the list.
29
86
# Find the index at which the user entered the number 99.
30
87
88
+
# list = []
31
89
90
+
# index = 0
32
91
92
+
# while len(list) < 10:
93
+
# user_input = list.append(int(input("Enter 10 numbers, one of which should be 99: ")))
94
+
95
+
# for each_number in list:
96
+
# if each_number != 99:
97
+
# index += 1
98
+
# else:
99
+
# print("99 is at index number " + str(index))
100
+
33
101
# 7. Print a multiplication table for the number 18 up to 15. E.g.
34
102
# 1 x 18 = 18
35
103
# 2 x 18 = 36
36
104
37
105
106
+
# for each_number in range(1, 16):
107
+
# print(str(each_number) + " x 18 = " + str(each_number*18))
108
+
109
+
# i = 0
110
+
111
+
# while i < 15:
112
+
# print(str(i) + " x 18 = " + str(i * 18))
113
+
# i += 1
38
114
39
115
# 8. Print the numbers 1 to 100 (including the number 100) using a while loop.
40
116
117
+
# i = 0
41
118
119
+
# while i < 101:
120
+
# print(i)
121
+
# i += 1
42
122
43
123
# 9. Rewrite question B8 from session 3 using a while loop
44
124
# - A school has following rules for their grading system:
@@ -50,13 +130,86 @@
50
130
# f. Below 25 - F
51
131
# Ask user to enter the lesson and the marks for three lessons and print out the corresponding grades for the lesson.
52
132
133
+
# lesson = None
134
+
135
+
# print("REPORT CARD:")
136
+
137
+
# while lesson != "":
138
+
# lesson = input("Input your lesson:\n")
139
+
# mark = int(input("Input your mark:\n"))
140
+
# if mark > 80:
141
+
# print(lesson + " - A grade")
142
+
# elif mark <= 80 and mark > 60:
143
+
# print(lesson + " - B grade")
144
+
# elif mark <= 60 and mark > 50:
145
+
# print(lesson + " - C grade")
146
+
# elif mark <= 50 and mark> 45:
147
+
# print(lesson + " - D grade")
148
+
# elif mark <= 45 and mark > 25:
149
+
# print(lesson + " - E grade")
150
+
# elif mark < 25:
151
+
# print(lesson + " - F grade")
152
+
# else:
153
+
# print("Go to see your teacher")
53
154
54
155
55
156
# 10. Ask the user to enter the names of people who entered a prize draw. Select a random winner and print their name
56
157
158
+
# import random
159
+
160
+
# list_of_names = []
161
+
162
+
# user_input = None
163
+
164
+
# while user_input != "":
165
+
# user_input = input("Enter the name of someone who entered a prize draw: ")
166
+
# list_of_names.append(user_input)
167
+
168
+
# # print(list_of_names)
169
+
# # print(len(list_of_names))
170
+
# del(list_of_names[len(list_of_names) - 1])
171
+
# # print(list_of_names)
172
+
# print("The winner is " + random.choice(list_of_names))
173
+
57
174
58
175
59
176
# 11. Create a rock, paper, scissors game which is run against computer.
60
177
# This is a game where you select either rock/paper/scissors and you compare it to your opponents choice.
61
178
# Rock beats scissors, paper beats rock, scissors beats paper. If both choose the same, then you play again.
62
179
# + Expand this so that its best of 3 games
180
+
181
+
importrandom
182
+
183
+
user_wins=0
184
+
computer_wins=0
185
+
options= ["Rock", "Paper", "Scissors"]
186
+
187
+
whileuser_wins<2andcomputer_wins<2:
188
+
user_input=input("Choose from Rock, Paper, or Scissors: ")
0 commit comments