'Sharing state between functions
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()
Solution 1:[1]
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 :-)
Solution 2:[2]
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.
Solution 3:[3]
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()
Solution 4:[4]
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()
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 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 |
