Skip to content

Commit 5903cc1

Browse files
committed
Day 17
- The Quiz Project & The Benefits of OOP - Some fixes in Day 16 code
1 parent ad46e7c commit 5903cc1

File tree

7 files changed

+67
-6
lines changed

7 files changed

+67
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
## 📚 Intermediate
2121
- [Day 15](https://github.com/a092devs/100-days-of-python/tree/master/day15) - Local Development Environment Setup & the Coffee Machine
2222
- [Day 16](https://github.com/a092devs/100-days-of-python/tree/master/day16) - Object Oriented Programming (OOP)
23+
- [Day 17](https://github.com/a092devs/100-days-of-python/tree/master/day17) - The Quiz Project & The Benefits of OOP
2324

2425

2526
## ⚙ Tools and Technologies Covered

day16/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
menu = Menu()
1010

1111
coffee_maker = CoffeeMaker()
12-
1312
money_machine = MoneyMachine()
1413

1514
while True:
16-
choice = input("What would you like? Choose between espresso, latte or cappuccino: ").lower()
15+
options = menu.get_items()
16+
choice = input(f"What would you like? ({options}): ").lower()
1717
if choice == 'off':
1818
print("Have a good day!")
1919
break
2020
elif choice == 'report':
2121
coffee_maker.report()
2222
money_machine.report()
2323
else:
24-
coffee_choice = menu.find_drink(choice)
25-
if coffee_maker.is_resource_sufficient(espresso) and money_machine.make_payment(espresso.cost):
26-
coffee_maker.make_coffee(coffee_choice)
24+
drink = menu.find_drink(choice)
25+
if coffee_maker.is_resource_sufficient(drink) and money_machine.make_payment(drink.cost):
26+
coffee_maker.make_coffee(drink)

day16/money_machine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def process_coins(self):
2020
"""Returns the total calculated from coins inserted."""
2121
print("Please insert coins.")
2222
for coin in self.COIN_VALUES:
23-
self.money_received += int(input(f"How many {coin}?: ")) * self.COIN_VALUES[coin]
23+
self.money_received += int(input(f"How many {coin}? ")) * self.COIN_VALUES[coin]
2424
return self.money_received
2525

2626
def make_payment(self, cost):

day17/data.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
question_data = [
2+
{"text": "A slug's blood is green.", "answer": "True"},
3+
{"text": "The loudest animal is the African Elephant.", "answer": "False"},
4+
{"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"},
5+
{"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"},
6+
{"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"},
7+
{"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"},
8+
{"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"},
9+
{"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"},
10+
{"text": "Google was originally called 'Backrub'.", "answer": "True"},
11+
{"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"},
12+
{"text": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"},
13+
{"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}
14+
]

day17/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from question_model import Question
2+
from data import question_data
3+
from quiz_brain import QuizBrain
4+
5+
question_bank = []
6+
for question in question_data:
7+
question_text = question["text"]
8+
question_answer = question["answer"]
9+
new_question = Question(question_text, question_answer)
10+
question_bank.append(new_question)
11+
12+
quiz = QuizBrain(question_bank)
13+
14+
while quiz.questions_left():
15+
quiz.next_question()
16+
17+
print(f"Thanks for completing the trivia. Your final score is: {quiz.score}/{quiz.question_number}.")

day17/question_model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Question:
2+
def __init__(self, text, answer):
3+
self.text = text
4+
self.answer = answer

day17/quiz_brain.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class QuizBrain:
2+
def __init__(self, question_list):
3+
self.question_number = 0
4+
self.question_list = question_list
5+
self.score = 0
6+
7+
def questions_left(self):
8+
return self.question_number < len(self.question_list)
9+
10+
def next_question(self):
11+
current_question = self.question_list[self.question_number]
12+
self.question_number += 1
13+
user_answer = input(f"Q.{self.question_number}: {current_question.text} (True/False): ")
14+
self.check_answer(user_answer, current_question.answer)
15+
16+
def check_answer(self, user_answer, correct_answer):
17+
if user_answer.lower() == correct_answer.lower():
18+
print("You got it right! 😁")
19+
self.score += 1
20+
else:
21+
print("That's incorrect! 😒")
22+
23+
print(f"The correct answer was: {correct_answer}.")
24+
print(f"Your current score is: {self.score}/{self.question_number}.")
25+

0 commit comments

Comments
 (0)