Home > database >  Sharing state between functions
Sharing state between functions

Time:02-26

I need to create a personality task for this assignment and I've written this block of code. I know it's probably not the best way but I would like to know why it is not working.

I have 3 questions which are all functions (Only 1 shown here) and their structures are all the same with the exception that instead of num_1, I used num_2 and num_3 for questions 2 and 3. I'm getting the error: '<' not supported between instances of 'int' and 'str. I have tried putting num_1 into an int(num_1) but that didn't work. Any help would be appreciated, thanks!

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1

def main():
    question1()
    if "num_1"   "num_2"   "num_3" == 9:
        print("Your favorite color is red")
    elif 5 < "num_1"   "num_2"   "num_3" < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()

CodePudding user response:

if "num_1"   "num_2"   "num_3" == 9:

You want the variables num_1, num_2, and num_3, not the strings "num_1", "num_2", and "num_3",

The expression "num_1" "num_2" "num_3" gives you the concatenated string "num_1num_2num_3" which you then try to compare with the integer 9, and that's why you're seeing:

... not supported between instances of 'int' and 'str'

Use this instead:

if num_1   num_2   num_3 == 9:

Additionally, it's not a good idea to rely on global variables like this (it doesn't work, for a start). It would be better for the question to return the value that you can use such as with:

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == 1:
        return 3
    music == 2:
        return 2
    return 1

def main():
    num_1 = question1()
    # and so on ...

Your code also has an issue in that you don't actually set num_2 or num_3 to anything but I'm guessing that will have something to do with the two other questions you intend to ask (that were left out of this simplified question).


I'd also question your assertion of a link between preferred music style and preferred color, but I'll let that slide :-)

CodePudding user response:

In your first function question1, the variable num1 is local to the function. It has no bearing on the state of the program once that function has finished running.

Strings like "num_1" are just strings, not references to variables with matching names.

CodePudding user response:

There are a few errors in your code:

  1. In your main() function, you put the variables num1, num2, and num3 in quotation marks in the if statements. This is incorrect because we want the compare the variable values, not the variable name.

  2. Even if you made functions to declare num2 and num3, because those variables (including num1) are local to the functions you would declare them in, they will not be recognized by your main() function.

To solve this problem, we can define num1, num2, and num3 as global variables.

With all these changes taken into account, your code could look something like this (with question2() and question3() modified of course so it's an actual personality quiz haha):

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    global num_1
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1

def question2():
    global num_2
    num_2 = 2

def question3():
    global num_3
    num_3 = 2

def main():
    question1()
    question2()
    question3()
    if num_1   num_2   num_3 == 9:
        print("Your favorite color is red")
    elif 5 < num_1   num_2   num_3 < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()

Alternate Solution (Preferred): Instead of defining the three variables as global, we can simply return them in our function, and define them in our main() function:

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1
    return num_1

def question2():
    num_2 = 2
    return num_2
    

def question3():
    num_3 = 2
    return num_3

def main():
    num_1 = question1()
    num_2 = question2()
    num_3 = question3()
    if num_1   num_2   num_3 == 9:
        print("Your favorite color is red")
    elif 5 < num_1   num_2   num_3 < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()

I hope this helped answer your question! Please let me know if you need any further clarification or details :)

CodePudding user response:

The return value of input is string do we need to test it against string type. You'd put your num variables in quotes in the tests, which I've corrected. The first part of the code runs now. It blocks on your tests to determine colour because you haven't defined the values of the variables that you want to add together. You need to complete your logic.

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")                                   
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == "1":
        num_1 = 3                                     
     elif music == "2":                                          
         num_1 = 2                                    
     else:
        num_1 = 1                                                                                   def main():                                           question1()
    if num_1   num_2   num_3 == 9:
        print("Your favorite color is red")             elif 5 < num_1   num_2   num_3 < 9:                   print("Your favorite color is blue")
    else:                                                 print("Your favorite color is green")                                                       main()

CodePudding user response:

A variable created inside a function belongs to only that function, and can only be used inside the function. Thats why you are getting an error num_1 not defined.

num_1 is defined in the function named question_1(). So it can't be accessed by any other function.

If you want to access the variable num_1 outside the function, you need to pass it to the next function or return the value or define the variable outside the function makes it a global variable. For reference:- You can read this

Your code will work like this:-

    def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1
    return num_1  #you should return your value used inside a function to access from outside. check line no.16

def main():
    num_1=question1()   #return your num_1 value here
    num_2=question2() #return your num_2 value here and so on...
    
    if num_1  num_2  num_3 == 9:
        print("Your favorite color is red")
    elif 5 < num_1   num_2   num_3 < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()
  • Related