Skip to content

Commit 6dbc05a

Browse files
committed
Add game logic
1 parent f5a6f7a commit 6dbc05a

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

web_scraping/scraping_project.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import requests
33
from bs4 import BeautifulSoup
44
from time import sleep
5+
from random import choice
56
from csv import writer
67

78
all_quotes = []
@@ -18,10 +19,36 @@
1819
all_quotes.append({
1920
"text": quote.find(class_="text").get_text(),
2021
"author": quote.find(class_="author").get_text(),
21-
"bio_link": quote.find("a")["href"]
22+
"bio-link": quote.find("a")["href"]
2223
})
2324
next_btn = soup.find(class_="next")
2425
url = next_btn.find("a")["href"] if next_btn else None
25-
sleep(3) # wait between scraping pages
26-
27-
print(all_quotes)
26+
# sleep(3) # wait between scraping pages
27+
28+
quote = choice(all_quotes)
29+
30+
guess = ""
31+
remaining_guesses = 4
32+
33+
print("Here's the quote: ")
34+
print(quote["text"])
35+
36+
while guess.lower() != quote["author"].lower() and remaining_guesses > 0:
37+
guess = input(f"Who said this quote? Guesses remaining: {remaining_guesses}\n")
38+
if guess.lower() == quote["author"].lower():
39+
print("YOU GOT IT RIGHT!")
40+
break
41+
remaining_guesses -= 1
42+
if remaining_guesses == 3:
43+
res = requests.get(f"{base_url}{quote['bio-link']}")
44+
soup = BeautifulSoup(res.text, "html.parser")
45+
birth_date = soup.find(class_="author-born-date").get_text()
46+
birth_place = soup.find(class_="author-born-location").get_text()
47+
print(f"Here's a hint: The author was born on {birth_date} {birth_place}")
48+
elif remaining_guesses == 2:
49+
print(f"Here's a hint: The author's first name starts with: {quote['author'][0]}")
50+
elif remaining_guesses == 1:
51+
last_initial = quote['author'].split(" ")[1][0]
52+
print(f"Here's a hint: The author's last name starts with: {last_initial}")
53+
else:
54+
print(f"Sorry, you ran out of guesses. The answer was {quote['author']}")

0 commit comments

Comments
 (0)