0

I am trying to make a program that recommends the user an activity to do after a short quiz. It takes tries to figure out how the user is feeling and assigns points to different moods depending on how they feel. At the end it recommends an activity depending on how many points the user received in each mood.

In this piece of code I am trying to add 1 to the 'stressed' variable but I keep receiving this error message "local variable 'stressed' referenced before assignment"

Here is my code

Questions = [
"Was your day stressful?\n(a) Yes\n(b) No\n(c) Kinda\n\n",
"Do you feel happy?",
"Are you sad?",]

happy = 0
sad = 0
stressed = 0

def question1():
    answer1 = input(Questions[0])
    if answer1 == "a" or "A":
        stressed += 1
        question2()
    if answer1 == "b" or "B":
        question2()
    if answer1 == "c" or "C":
        stressed += 0.5
        question2()

question1()

Any help with this would be greatly appreciated. Thanks in advance.

djs
  • 3,947
  • 3
  • 14
  • 28
Hannan
  • 37
  • 5
  • 3
    As written, your code is not valid Python (indentation issues). Please [edit] your post to ensure it matches the code you're actually using. – jtbandes May 10 '20 at 02:22
  • 1
    `global stressed` – Barmar May 10 '20 at 02:24
  • 1
    Also, `if answer1 == "a" or "A":` is not correct. It should be `if answer1 in ("a", "A"):` or `if answer1.lower() == "a":` – Barmar May 10 '20 at 02:25

1 Answers1

0

Stressed was not recognised in your question1() function. Thus you can add

global stressed

in question1() to make sure it references the stressed variable you created outside the function

def question1():
    global stressed
    answer1 = input(Questions[0])
    if answer1 == "a" or "A":
    stressed += 1
    question2()
if answer1 == "b" or "B":
    question2()
if answer1 == "c" or "C":
    stressed += 0.5
    question2()
Jackson
  • 1,213
  • 1
  • 4
  • 14