Skip to content

Instantly share code, notes, and snippets.

@LazarLazarov
Created June 3, 2023 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LazarLazarov/689b1b1aafc1727176519cd3025f5385 to your computer and use it in GitHub Desktop.
Save LazarLazarov/689b1b1aafc1727176519cd3025f5385 to your computer and use it in GitHub Desktop.
import random
class Question:
def __init__(self, text, answers, category):
self.text = text
self.answers = answers
self.category = category
class Answer:
def __init__(self, text, score, category):
self.text = text
self.score = score
self.category = category
questions = [
Question("The project you are in charge of is a great success.", [Answer("I kept a close watch over everyone’s work.", 0, "PsG"), Answer("Everyone devoted a lot of time and energy to it.", 1, "PmG")], "PsG"),
Question("You and your spouse (boyfriend/girlfriend) make up after a fight.", [Answer("I forgave him/her.", 0, "PmG"), Answer("I’m usually forgiving.", 1, "PsG")], "PmG"),
Question("You get lost driving to a friend’s house.", [Answer("I missed a turn.", 1, "PsB"), Answer("My friend gave me bad directions.", 0, "PmB")], "PsB"),
Question("Your spouse (boyfriend/girlfriend) surprises you with a gift.", [Answer("He/she just got a raise at work.", 0, "PsG"), Answer("I took him/her out to a special dinner the night before.", 1, "PmG")], "PsG"),
Question("You forget your spouse’s (boyfriend’s/girlfriend’s) birthday.", [Answer("I’m not good at remembering birthdays.", 1, "PmB"), Answer("I was preoccupied with other things.", 0, "PsB")], "PmB"),
Question("You get a flower from a secret admirer.", [Answer("I am attractive to him/her.", 0, "PvG"), Answer("I am a popular person.", 1, "PmG")], "PvG"),
Question("You run for a community office position and you win.", [Answer("I devote a lot of time and energy to campaigning.", 0, "PvG"), Answer("I work very hard at everything I do.", 1, "PmG")], "PvG"),
Question("You miss an important engagement.", [Answer("Sometimes my memory fails me.", 1, "PvB"), Answer("I sometimes forget to check my appointment book.", 0, "PmB")], "PvB"),
Question("You run for a community office position and you lose.", [Answer("I didn’t campaign hard enough.", 1, "PsB"), Answer("The person who won knew more people.", 0, "PmB")], "PsB"),
Question("You host a successful dinner.", [Answer("I was particularly charming that night.", 0, "PmG"), Answer("I am a good host.", 1, "PsG")], "PmG"),
Question("You stop a crime by calling the police.", [Answer("A strange noise caught my attention.", 0, "PsG"), Answer("I was alert that day.", 1, "PmG")], "PsG"),
Question("You were extremely healthy all year.", [Answer("Few people around me were sick, so I wasn’t exposed", 0, "PsG"), Answer("I made sure I ate well and got enough rest.", 1, "PmG")], "PsG"),
Question("You owe the library ten dollars for an overdue book.", [Answer("When I am really involved in what I am reading, I often forget when it’s due.", 1, "PmB"), Answer("I was so involved in writing the report that I forgot to return the book.", 0, "PsB")], "PmB"),
Question("Your stocks make you a lot of money.", [Answer("My broker decided to take on something new.", 0, "PmG"), Answer("My broker is a top-notch investor.", 1, "PsG")], "PmG"),
Question("You win an athletic contest.", [Answer("I was feeling unbeatable.", 0, "PmG"), Answer("I train hard.", 1, "PsG")], "PmG"),
Question("You fail an important examination.", [Answer("I wasn’t as smart as the other people taking the exam.", 1, "PvB"), Answer("I didn’t prepare for it well.", 0, "PmB")], "PvB"),
Question("You prepared a special meal for a friend and he/she barely touched the food.", [Answer("I wasn’t a good cook.", 1, "PvB"), Answer("I made the meal in a rush.", 0, "PmB")], "PvB"),
Question("You lose a sporting event for which you have been training for a long time.", [Answer("I’m not very athletic.", 1, "PvB"), Answer("I’m not good at that sport.", 0, "PmB")], "PvB"),
Question("Your car runs out of gas on a dark street late at night.", [Answer("I didn’t check to see how much gas was in the tank.", 1, "PsB"), Answer("The gas gauge was broken.", 0, "PmB")], "PsB"),
Question("You lose your temper with a friend.", [Answer("He/she is always nagging me.", 1, "PmB"), Answer("He/she was in a hostile mood.", 0, "PsB")], "PmB"),
Question("You are penalized for not returning your income-tax forms on time.", [Answer("I always put off doing my taxes.", 1, "PmB"), Answer("I was lazy about getting my taxes done this year.", 0, "PsB")], "PmB"),
Question("You ask a person out on a date and he/she says no.", [Answer("I was a wreck that day.", 1, "PvB"), Answer("I got tongue-tied when I asked him/her on the date.", 0, "PmB")], "PvB"),
Question("A game-show host picks you out of the audience to participate in the show.", [Answer("I was sitting in the right seat.", 0, "PsG"), Answer("I looked the most enthusiastic.", 1, "PmG")], "PsG"),
Question("You are frequently asked to dance at a party.", [Answer("I am outgoing at parties.", 1, "PmG"), Answer("I was in perfect form that night.", 0, "PsG")], "PmG"),
Question("You buy your spouse (boyfriend/girlfriend) a gift and he/she doesn’t like it.", [Answer("I don’t put enough thought into things like that.", 1, "PsB"), Answer("He/she has very picky tastes.", 0, "PmB")], "PsB"),
Question("You do exceptionally well in a job interview.", [Answer("I felt extremely confident during the interview.", 0, "PmG"), Answer("I interview well.", 1, "PsG")], "PmG"),
Question("You tell a joke and everyone laughs.", [Answer("The joke was funny.", 0, "PsG"), Answer("My timing was perfect.", 1, "PmG")], "PsG"),
Question("Your boss gives you too little time in which to finish a project, but you get it finished anyway.", [Answer("I am good at my job.", 0, "PvG"), Answer("I am an efficient person.", 1, "PmG")], "PvG"),
Question("You’ve been feeling run-down lately.", [Answer("I never get a chance to relax.", 1, "PmB"), Answer("I was exceptionally busy this week.", 0, "PsB")], "PmB"),
Question("You ask someone to dance and he/she says no.", [Answer("I am not a good enough dancer.", 1, "PsB"), Answer("He/she doesn’t like to dance.", 0, "PmB")], "PsB"),
Question("You save a person from choking to death.", [Answer("I know a technique to stop someone from choking.", 0, "PvG"), Answer("I know what to do in crisis situations.", 1, "PmG")], "PvG"),
Question("Your romantic partner wants to cool things off for a while.", [Answer("I’m too self-centered.", 1, "PvB"), Answer("I don’t spend enough time with him/her.", 0, "PmB")], "PvB"),
Question("A friend says something that hurts your feelings.", [Answer("She always blurts things out without thinking of others.", 1, "PmB"), Answer("My friend was in a bad mood and took it out on me.", 0, "PsB")], "PmB"),
Question("Your employer comes to you for advice.", [Answer("I am an expert in the area about which I was asked.", 0, "PvG"), Answer("I am good at giving useful advice.", 1, "PmG")], "PvG"),
Question("A friend thanks you for helping him/her get through a bad time.", [Answer("I enjoy helping him/her through tough times.", 0, "PvG"), Answer("I care about people.", 1, "PmG")], "PvG"),
Question("You have a wonderful time at a party.", [Answer("Everyone was friendly.", 0, "PsG"), Answer("I was friendly.", 1, "PmG")], "PsG"),
Question("Your doctor tells you that you are in good physical shape.", [Answer("I make sure I exercise frequently.", 0, "PvG"), Answer("I am very health-conscious.", 1, "PmG")], "PvG"),
Question("Your spouse (boyfriend/girlfriend) takes you away for a romantic weekend.", [Answer("He/she needed to get away for a few days.", 0, "PmG"), Answer("He/she likes to explore new areas.", 1, "PsG")], "PmG"),
Question("Your doctor tells you that you eat too much sugar.", [Answer("I don’t pay much attention to my diet.", 1, "PsB"), Answer("You can’t avoid sugar, it’s in everything.", 0, "PmB")], "PsB"),
Question("You are asked to head an important project.", [Answer("I just successfully completed a similar project.", 0, "PmG"), Answer("I am a good supervisor.", 1, "PsG")], "PmG"),
Question("You and your spouse (boyfriend/girlfriend) have been fighting a great deal.", [Answer("I have been feeling cranky and pressured lately.", 1, "PsB"), Answer("He/she has been hostile lately.", 0, "PmB")], "PsB"),
Question("You fall down a great deal while skiing.", [Answer("Skiing is difficult.", 1, "PmB"), Answer("The trails were icy.", 0, "PsB")], "PmB"),
Question("You win a prestigious award.", [Answer("I solved an important problem.", 0, "PvG"), Answer("I was the best employee.", 1, "PmG")], "PvG"),
Question("Your stocks are at an all-time low.", [Answer("I didn’t know much about the business climate at the time.", 1, "PvB"), Answer("I made a poor choice of stocks.", 0, "PmB")], "PvB"),
Question("You win the lottery.", [Answer("It was pure chance.", 0, "PsG"), Answer("I picked the right numbers.", 1, "PmG")], "PsG"),
Question("You gain weight over the holidays and you can’t lose it.", [Answer("Diets don’t work in the long run.", 1, "PmB"), Answer("The diet I tried didn’t work.", 0, "PsB")], "PmB"),
Question("You are in the hospital and few people come to visit.", [Answer("I’m irritable when I am sick.", 1, "PsB"), Answer("My friends are negligent about things like that.", 0, "PmB")], "PsB"),
Question("They won’t honor your credit card at a store.", [Answer("I sometimes overestimate how much money I have.", 1, "PvB"), Answer("I sometimes forget to pay my credit-card bill.", 0, "PmB")], "PvB")
]
def interpret_score(score, ranges):
for range, interpretation in ranges:
if score in range:
return interpretation
# Interpretation ranges
interpretation_ranges = {
"PmB": [(range(0, 2), "very optimistic"), (range(2, 4), "moderately optimistic"), (range(4, 5), "average"), (range(5, 7), "quite pessimistic"), (range(7, 9), "very pessimistic")],
"PmG": [(range(7, 9), "very optimistic"), (range(6, 7), "moderately optimistic"), (range(4, 6), "average"), (range(3, 4), "moderately pessimistic"), (range(0, 3), "very pessimistic")],
"PvB": [(range(0, 2), "very optimistic"), (range(2, 4), "moderately optimistic"), (range(4, 5), "average"), (range(5, 7), "moderately pessimistic"), (range(7, 9), "very pessimistic")],
"PvG": [(range(7, 9), "very optimistic"), (range(6, 7), "moderately optimistic"), (range(4, 6), "average"), (range(3, 4), "moderately pessimistic"), (range(0, 3), "very pessimistic")],
"PsB": [(range(0, 2), "very high self-esteem"), (range(2, 4), "moderate self-esteem"), (range(4, 5), "average"), (range(5, 7), "moderately low self-esteem"), (range(7, 9), "very low self-esteem")],
"PsG": [(range(7, 9), "very optimistic"), (range(6, 7), "moderately optimistic"), (range(4, 6), "average"), (range(3, 4), "moderately pessimistic"), (range(0, 3), "very pessimistic")],
"HoB": [(range(0, 3), "extraordinarily hopeful"), (range(3, 7), "moderately hopeful"), (range(7, 9), "average"), (range(9, 12), "moderately hopeless"), (range(12, 17), "severely hopeless")],
"Total B": [(range(3, 7), "marvelously optimistic"), (range(7, 10), "moderately optimistic"), (range(10, 12), "average"), (range(12, 15), "moderately pessimistic"), (range(15, 25), "very pessimistic")],
"Total G": [(range(19, 25), "very optimistic"), (range(17, 20), "moderately optimistic"), (range(14, 17), "average"), (range(11, 14), "quite pessimistic"), (range(0, 11), "great pessimism")],
"G-B": [(range(9, 25), "very optimistic"), (range(6, 9), "moderately optimistic"), (range(3, 6), "average"), (range(1, 3), "moderately pessimistic"), (range(-24, 1), "very pessimistic")]
}
# Interpretation descriptions
interpretation_descriptions = {
"PmB": "Permanent bad events (PmB)",
"PmG": "Permanent good events (PmG)",
"PvB": "Pervasive bad events (PvB)",
"PvG": "Pervasive good events (PvG)",
"PsB": "Personal bad events (PsB)",
"PsG": "Personal good events (PsG)",
"HoB": "Hope (HoB)",
"Total B": "Total bad events (Total B)",
"Total G": "Total good events (Total G)",
"G-B": "Difference between good and bad events (G-B)"
}
def administer_test(questions):
scores = {"PmB": 0, "PmG": 0, "PvB": 0, "PvG": 0, "PsB": 0, "PsG": 0}
random.shuffle(questions)
responses = [None] * len(questions) # Initialize a list to store the user's responses
i = 0
while i < len(questions):
question = questions[i]
print(f"{i+1}. {question.text}")
for j, answer in enumerate(question.answers):
print(f"{chr(65+j)}. {answer.text}") # chr(65) is 'A', chr(66) is 'B'
while True:
response = input("Enter the letter of your answer or 'P' to go back to the previous question: ").upper()
if len(response) == 1 and response in ['A', 'B', 'P']:
break
else:
print("Invalid input. Please enter either 'A' for the first answer, 'B' for the second answer, or 'P' to go back.")
if response == 'P':
if i > 0:
i -= 1
# Subtract the score from the previous answer
previous_response_index = ord(responses[i]) - 65
previous_answer = questions[i].answers[previous_response_index]
scores[previous_answer.category] -= previous_answer.score
continue
response_index = ord(response) - 65 # ord('A') is 65, ord('B') is 66
responses[i] = response # Store the user's response
answer = question.answers[response_index]
scores[question.category] += answer.score
i += 1
scores["HoB"] = scores["PvB"] + scores["PmB"] # calculate the "HoB" score
scores["Total B"] = scores["PvB"] + scores["PmB"] + scores["PsB"]
scores["Total G"] = scores["PvG"] + scores["PmG"] + scores["PsG"]
scores["G-B"] = scores["Total G"] - scores["Total B"]
return scores
scores = administer_test(questions)
# Prettified output with interpretations
print("\nYour Optimism Scores:")
print("---------------------")
for score_name, score_value in scores.items():
print(f"{interpretation_descriptions[score_name]}: {score_value} ({interpret_score(score_value, interpretation_ranges[score_name])})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment