-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
75 lines (61 loc) · 2.76 KB
/
game.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import random
def load_data(filepath):
# Load the dataset from the given file path, assuming each line is a separate JSON object
headlines_data = []
with open(filepath, 'r') as file:
for line in file:
headlines_data.append(json.loads(line))
return headlines_data
def play_game(headlines_data, number_of_questions):
# Shuffle the data to get random headlines
random.shuffle(headlines_data)
# Initialize score and a list to keep track of incorrect guesses for summary
score = 0
incorrect_guesses = []
# Start the game loop
for i in range(number_of_questions):
headline = headlines_data[i]
print(f"\nHeadline {i+1}: {headline['headline']}")
print("Is this headline from The Onion (type 'o') or the Huffington Post (type 'h')?")
user_guess = ''
while user_guess not in ['o', 'h']:
user_guess = input("Your guess: ").lower()
if user_guess not in ['o', 'h']:
print("Invalid input. Please type 'o' for The Onion or 'h' for the Huffington Post.")
# Check the answer and record incorrect guesses
correct_answer = 'o' if headline['is_sarcastic'] == 1 else 'h'
if user_guess != correct_answer:
incorrect_guesses.append((i+1, headline['headline'], correct_answer))
score += user_guess == correct_answer
# Final score and summary
incorrect_count = len(incorrect_guesses)
success_rate = score / number_of_questions * 100
print("\nGame Over! Here's your summary:")
print(f"Number correct: {score}")
print(f"Number incorrect: {incorrect_count}")
print(f"Success rate: {success_rate:.2f}%")
# If there are incorrect guesses, show them
if incorrect_guesses:
print("\nHere are the headlines you guessed incorrectly:")
for index, headline, correct_answer in incorrect_guesses:
print(f"{index}. {headline} (Correct answer: {'The Onion' if correct_answer == 'o' else 'The Huffington Post'})")
def main():
# File path to the dataset
filepath = 'Sarcasm_Headlines_Dataset_v2.json' # Update with the correct path
headlines_data = load_data(filepath)
question_options = {
'1': 20,
'2': 50,
'3': 100
}
number_of_questions = 0
while number_of_questions == 0:
number_of_questions_input = input("How many questions do you want to answer?\nType '1' for 20, '2' for 50, '3' for 100 questions: ")
if number_of_questions_input in question_options:
number_of_questions = question_options[number_of_questions_input]
else:
print("Invalid choice. Please type '1', '2', or '3'.")
play_game(headlines_data, number_of_questions)
if __name__ == "__main__":
main()