-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuotesGuessingGame.py
61 lines (58 loc) · 2.13 KB
/
QuotesGuessingGame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#http://quotes.toscrape.com
import requests
from bs4 import BeautifulSoup
from random import choice
BASE_URL = "http://quotes.toscrape.com"
def scrape_quotes():
all_quotes = []
url = "/page/1"
while url:
res = requests.get(f"{BASE_URL}{url}")
# print(f"Now Scrapping {BASE_URL}{url}....")
soup = BeautifulSoup(res.text,"html.parser")
quotes = soup.find_all(class_="quote")
for quote in quotes:
all_quotes.append({
"text": quote.find(class_="text").get_text(),
"author": quote.find(class_="author").get_text(),
"bio-link": quote.find("a")["href"]
})
next_btn = soup.find(class_="next")
url = next_btn.find("a")["href"] if next_btn else None
return all_quotes
def start_game(quotes):
quote = choice(quotes)
remaining_guesses = 4
print("Here's a quote: ")
print(quote["text"])
# print(quote["author"])
guess = ''
while guess.lower() != quote["author"].lower() and remaining_guesses > 0:
guess = input(f"Who said this quote? Guesses remaining: {remaining_guesses} ")
if guess.lower() == quote["author"].lower():
print("YOU GOT IT RIGHT!")
break
remaining_guesses -= 1
if remaining_guesses == 3:
res = requests.get(f"{BASE_URL}{quote['bio-link']}")
soup = BeautifulSoup(res.text,"html.parser")
birth_date = soup.find(class_="author-born-date").get_text()
birth_place = soup.find(class_="author-born-location").get_text()
print(f"Here's a hint: The author was born in {birth_date} in {birth_place}")
elif remaining_guesses == 2:
print(f"Here's a hint: The author first name starts with {quote['author'][0]}")
elif remaining_guesses == 1:
last_initial = quote["author"].split(" ")[1][0]
print(f"Here's a hint: The author last name starts with {last_initial}")
else:
print(f"Sorry you ran out of guesses. The answer was {quote['author']}")
again = ''
while again.lower() not in ('y','yes','n','no'):
again = input("Would you like to play again (y/n) ")
if again.lower() in ('y','yes'):
print("OK YOU PLAY AGAIN....")
return start_game(quotes)
else:
print("OK GOODBYE!")
quotes = scrape_quotes()
start_game(quotes)