我正在尝试编写一个程序,向用户推荐经过简短测验的活动。需要尝试弄清楚用户的感觉,并根据他们的感觉为不同的情绪分配分数。最后,根据用户在每种心情中获得的分数,推荐一项活动。
在这段代码中,我试图在'stressed'变量中加1,但我不断收到此错误消息“分配前引用了本地变量'stressed']
这是我的代码
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()
在此方面的任何帮助将不胜感激。预先感谢。
您的问题1()函数中未识别到压力。因此,您可以添加
global stressed
在question1()中,确保它引用了您在函数外部创建的压力变量
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()