'Getting a not defined error despite setting my values to chnage depending on what the user inputs

Here is my code. It is a pretty simple piece that changes the values of the first, second and third letter snippets depending on whether the user answers yes or no to the questions. For some reason I keep on getting errors that my letter values are not defined. Please let me know how to fix this issue.

code:

import webbrowser as wb
question1 = input("Do you like to go out with others?")
question2 = input("Are you a relatively happy person?")
question3 = input("Are you often sad?")
question4 = input("Would you say you live better than most people?")
def personality_test():
    first_letter = 0 
    second_letter = 0
    third_letter = 0
    
    if question1 == "Yes":
        first_letter = 2
    if question1 == "yes":
        first_letter = 2
    if question1 == "No":
        first_letter = 1
        
    if question1 == "no":
        first_letter = 1
        
    if question2 == "Yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2
        
        if question4 == "yes":
            third_letter = 2
            
    if question2 == "yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2
            
        if question4 == "yes":
            third_letter = 2
    
    else:
        if question3 == "yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")
        
        if question3 == "Yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")
            
            
            
            
       
personality_result = (first_letter + second_letter + third_letter)
personality_test()


Solution 1:[1]

The variables within the function are not global, so you cannot use them outside the function.

Code fix:

import webbrowser as wb

question1 = input("Do you like to go out with others?")
question2 = input("Are you a relatively happy person?")
question3 = input("Are you often sad?")
question4 = input("Would you say you live better than most people?")
first_letter = 0
second_letter = 0
third_letter = 0

def personality_test():
    global first_letter
    global second_letter
    global third_letter
    first_letter = 0
    second_letter = 0
    third_letter = 0

    if question1 == "Yes":
        first_letter = 2
    if question1 == "yes":
        first_letter = 2
    if question1 == "No":
        first_letter = 1

    if question1 == "no":
        first_letter = 1

    if question2 == "Yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2

        if question4 == "yes":
            third_letter = 2

    if question2 == "yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2

        if question4 == "yes":
            third_letter = 2

    else:
        if question3 == "yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")

        if question3 == "Yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")


personality_result = (first_letter + second_letter + third_letter)
personality_test()

I just ran this and it works great!

Solution 2:[2]

This should work:

import webbrowser as wb

question1 = input("Do you like to go out with others?")
question2 = input("Are you a relatively happy person?")
question3 = input("Are you often sad?")
question4 = input("Would you say you live better than most people?")

def personality_test():
    global first_letter
    global second_letter
    global third_letter
    first_letter = 0
    second_letter = 0
    third_letter = 0

    if question1 == "Yes":
        first_letter = 2
    if question1 == "yes":
        first_letter = 2
    if question1 == "No":
        first_letter = 1

    if question1 == "no":
        first_letter = 1

    if question2 == "Yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2

        if question4 == "yes":
            third_letter = 2

    if question2 == "yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2

        if question4 == "yes":
            third_letter = 2

    else:
        if question3 == "yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")

        if question3 == "Yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")


personality_test()
personality_result = (first_letter + second_letter + third_letter)

The variables you defined are not global and there is no need to define them at the top of the program. Also, you have to run personality_test() before you add up the letters to get the personality_result

Solution 3:[3]

Your first, second and third letter variables are defined in the personality_test() function. They only exist in that scope.

Instead of the other suggestions of making them global variables. Just return the result from the function

import webbrowser as wb

question1 = input("Do you like to go out with others?")
question2 = input("Are you a relatively happy person?")
question3 = input("Are you often sad?")
question4 = input("Would you say you live better than most people?")

def personality_test():
    first_letter = 0 
    second_letter = 0
    third_letter = 0
    
    if question1 == "Yes":
        first_letter = 2
    if question1 == "yes":
        first_letter = 2
    if question1 == "No":
        first_letter = 1
        
    if question1 == "no":
        first_letter = 1
        
    if question2 == "Yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2
        
        if question4 == "yes":
            third_letter = 2
            
    if question2 == "yes":
        second_letter = 2
        if question4 == "Yes":
            third_letter = 2
            
        if question4 == "yes":
            third_letter = 2
    
    else:
        if question3 == "yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")
        
        if question3 == "Yes":
            print("Please contact this link. I hope you will be alright.")
            wb.open("https://suicidepreventionlifeline.org/")
            
    result = (first_letter + second_letter + third_letter)
    return result
            
            
       
personality_result = personality_test()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Blue Robin
Solution 2 catasaurus
Solution 3 JamesRoberts