Skip to content

Commit b57d4a8

Browse files
committed
Add examples
1 parent ce13f44 commit b57d4a8

File tree

117 files changed

+6475
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+6475
-0
lines changed

Finished/Basics/01/quiz.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Example file for LinkedIn Learning Course "Python: Build a Quiz App" by Joe Marini
2+
# The Quiz and Question classes define a particular quiz
3+
4+
5+
class Question:
6+
def __init__(self):
7+
self.points = 0
8+
self.correct_answer = ""
9+
self.text = ""
10+
self.is_correct = False
11+
12+
13+
class QuestionTF(Question):
14+
def __init__(self):
15+
super().__init__()
16+
17+
def ask(self):
18+
while (True):
19+
print(f"(T)rue or (F)alse: {self.text}")
20+
response = input("? ")
21+
22+
if (len(response) == 0):
23+
print("Sorry, that's not a valid response. Please try again")
24+
continue
25+
26+
response = response.lower()
27+
if (response[0] != "t" and response[0] != "f"):
28+
print("Sorry, that's not a valid response. Please try again")
29+
continue
30+
31+
if response[0] == self.correct_answer:
32+
self.is_correct = True
33+
34+
break
35+
36+
37+
class QuestioncMC(Question):
38+
def __init__(self):
39+
super().__init__()
40+
self.answers = []
41+
42+
def ask(self):
43+
while (True):
44+
print(self.text)
45+
for a in self.answers:
46+
print(f"{a.name}) {a.text}")
47+
48+
response = input("? ")
49+
50+
if (len(response) == 0):
51+
print("Sorry, that's not a valid response. Please try again")
52+
continue
53+
54+
response = response.lower()
55+
if response[0] == self.correct_answer:
56+
self.is_correct = True
57+
58+
break
59+
60+
61+
class Answer:
62+
def __init__(self):
63+
self.text = ""
64+
self.name = ""
65+
66+
if __name__ == "__main__":
67+
q1 = QuestionTF()
68+
q1.text = "Broccoli is good for you"
69+
q1.points = 5
70+
q1.correct_answer = "t"
71+
q1.ask()
72+
q2 = QuestioncMC()
73+
q2.text = "What is 2+2?"
74+
q2.points = 10
75+
q2.correct_answer = "b"
76+
ans = Answer()
77+
ans.name = "a"
78+
ans.text = "3"
79+
q2.answers.append(ans)
80+
ans = Answer()
81+
ans.name = "b"
82+
ans.text = "4"
83+
q2.answers.append(ans)
84+
ans = Answer()
85+
ans.name = "c"
86+
ans.text = "5"
87+
q2.answers.append(ans)
88+
q2.ask()
89+
90+
print(q1.is_correct)
91+
print(q2.is_correct)
92+

Finished/Basics/02/quiz.py

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Example file for LinkedIn Learning Course "Python: Build a Quiz App" by Joe Marini
2+
# The Quiz and Question classes define a particular quiz
3+
4+
5+
class Quiz:
6+
def __init__(self):
7+
self.name = ""
8+
self.description = ""
9+
self.questions = []
10+
self.score = 0
11+
self.correct_count = 0
12+
self.total_points = 0
13+
14+
def print_header(self):
15+
print("\n\n*******************************************")
16+
print(f"QUIZ NAME: {self.name}")
17+
print(f"DESCRIPTION: {self.description}")
18+
print(f"QUESTIONS: {len(self.questions)}")
19+
print(f"TOTAL POINTS: {self.total_points}")
20+
print("*******************************************\n")
21+
22+
def print_results(self):
23+
print("*******************************************")
24+
25+
print("*******************************************\n")
26+
27+
def take_quiz(self):
28+
# initialize the quiz state
29+
self.score = 0
30+
self.correct_count = 0
31+
for q in self.questions:
32+
q.is_correct = False
33+
34+
# print the header
35+
self.print_header()
36+
37+
# execute each question and record the result
38+
for q in self.questions:
39+
q.ask()
40+
if (q.is_correct):
41+
self.correct_count += 1
42+
self.score += q.points
43+
print("------------------------------------------------\n")
44+
45+
# return the results
46+
return (self.score, self.correct_count, self.total_points)
47+
48+
49+
class Question:
50+
def __init__(self):
51+
self.points = 0
52+
self.correct_answer = ""
53+
self.text = ""
54+
self.is_correct = False
55+
56+
57+
class QuestionTF(Question):
58+
def __init__(self):
59+
super().__init__()
60+
61+
def ask(self):
62+
while (True):
63+
print(f"(T)rue or (F)alse: {self.text}")
64+
response = input("? ")
65+
66+
if (len(response) == 0):
67+
print("Sorry, that's not a valid response. Please try again")
68+
continue
69+
70+
response = response.lower()
71+
if (response[0] != "t" and response[0] != "f"):
72+
print("Sorry, that's not a valid response. Please try again")
73+
continue
74+
75+
if response[0] == self.correct_answer:
76+
self.is_correct = True
77+
78+
break
79+
80+
81+
class QuestioncMC(Question):
82+
def __init__(self):
83+
super().__init__()
84+
self.answers = []
85+
86+
def ask(self):
87+
while (True):
88+
print(self.text)
89+
for a in self.answers:
90+
print(f"{a.name}) {a.text}")
91+
92+
response = input("? ")
93+
94+
if (len(response) == 0):
95+
print("Sorry, that's not a valid response. Please try again")
96+
continue
97+
98+
response = response.lower()
99+
if response[0] == self.correct_answer:
100+
self.is_correct = True
101+
102+
break
103+
104+
105+
class Answer:
106+
def __init__(self):
107+
self.text = ""
108+
self.name = ""
109+
110+
111+
if __name__ == "__main__":
112+
qz = Quiz()
113+
qz.name = "Sample Quiz"
114+
qz.description = "This is a sample quiz!"
115+
116+
q1 = QuestionTF()
117+
q1.text = "Broccoli is good for you"
118+
q1.points = 5
119+
q1.correct_answer = "t"
120+
qz.questions.append(q1)
121+
122+
q2 = QuestioncMC()
123+
q2.text = "What is 2+2?"
124+
q2.points = 10
125+
q2.correct_answer = "b"
126+
ans = Answer()
127+
ans.name = "a"
128+
ans.text = "3"
129+
q2.answers.append(ans)
130+
ans = Answer()
131+
ans.name = "b"
132+
ans.text = "4"
133+
q2.answers.append(ans)
134+
ans = Answer()
135+
ans.name = "c"
136+
ans.text = "5"
137+
q2.answers.append(ans)
138+
qz.questions.append(q2)
139+
140+
qz.total_points = q1.points + q2.points
141+
result = qz.take_quiz()
142+
print(result)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<QuizML name="Sample Quiz">
3+
<Description>This is a sample quiz. There are some sample questions and such,
4+
but nothing too difficult (it is, after all, a sample).</Description>
5+
<Question type="multichoice" points="10">
6+
<QuestionText answer="2">To the nearest billion, how old is the earth?</QuestionText>
7+
<Answer name="1">3 billion years</Answer>
8+
<Answer name="2">5 billion years</Answer>
9+
<Answer name="3">7 billion years</Answer>
10+
<Answer name="4">9 billion years</Answer>
11+
</Question>
12+
<Question type="tf" points="5">
13+
<QuestionText answer="t">Broccoli is good for you</QuestionText>
14+
</Question>
15+
<Question type="tf" points="3">
16+
<QuestionText answer="f">The world is flat</QuestionText>
17+
</Question>
18+
<Question type="multichoice" points="10">
19+
<QuestionText answer="3">What was the color of George Washington's White Horse?</QuestionText>
20+
<Answer name="1">Brown</Answer>
21+
<Answer name="2">Black</Answer>
22+
<Answer name="3">White</Answer>
23+
<Answer name="4">Gray</Answer>
24+
</Question>
25+
</QuizML>

Finished/Basics/03/quiz.py

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Example file for LinkedIn Learning Course "Python: Build a Quiz App" by Joe Marini
2+
# The Quiz and Question classes define a particular quiz
3+
4+
5+
class Quiz:
6+
def __init__(self):
7+
self.name = ""
8+
self.description = ""
9+
self.questions = []
10+
self.score = 0
11+
self.correct_count = 0
12+
self.total_points = 0
13+
14+
def print_header(self):
15+
print("\n\n*******************************************")
16+
print(f"QUIZ NAME: {self.name}")
17+
print(f"DESCRIPTION: {self.description}")
18+
print(f"QUESTIONS: {len(self.questions)}")
19+
print(f"TOTAL POINTS: {self.total_points}")
20+
print("*******************************************\n")
21+
22+
def print_results(self):
23+
print("*******************************************")
24+
25+
print("*******************************************\n")
26+
27+
def take_quiz(self):
28+
# initialize the quiz state
29+
self.score = 0
30+
self.correct_count = 0
31+
for q in self.questions:
32+
q.is_correct = False
33+
34+
# print the header
35+
self.print_header()
36+
37+
# execute each question and record the result
38+
for q in self.questions:
39+
q.ask()
40+
if (q.is_correct):
41+
self.correct_count += 1
42+
self.score += q.points
43+
print("------------------------------------------------\n")
44+
45+
# return the results
46+
return (self.score, self.correct_count, self.total_points)
47+
48+
49+
class Question:
50+
def __init__(self):
51+
self.points = 0
52+
self.correct_answer = ""
53+
self.text = ""
54+
self.is_correct = False
55+
56+
57+
class QuestionTF(Question):
58+
def __init__(self):
59+
super().__init__()
60+
61+
def ask(self):
62+
while (True):
63+
print(f"(T)rue or (F)alse: {self.text}")
64+
response = input("? ")
65+
66+
if (len(response) == 0):
67+
print("Sorry, that's not a valid response. Please try again")
68+
continue
69+
70+
response = response.lower()
71+
if (response[0] != "t" and response[0] != "f"):
72+
print("Sorry, that's not a valid response. Please try again")
73+
continue
74+
75+
if response[0] == self.correct_answer:
76+
self.is_correct = True
77+
78+
break
79+
80+
81+
class QuestioncMC(Question):
82+
def __init__(self):
83+
super().__init__()
84+
self.answers = []
85+
86+
def ask(self):
87+
while (True):
88+
print(self.text)
89+
for a in self.answers:
90+
print(f"{a.name}) {a.text}")
91+
92+
response = input("? ")
93+
94+
if (len(response) == 0):
95+
print("Sorry, that's not a valid response. Please try again")
96+
continue
97+
98+
response = response.lower()
99+
if response[0] == self.correct_answer:
100+
self.is_correct = True
101+
102+
break
103+
104+
105+
class Answer:
106+
def __init__(self):
107+
self.text = ""
108+
self.name = ""

0 commit comments

Comments
 (0)