I am making a python script that suggests new music to you, but for some reason i am getting a lot of errors. Script is not finished, but here it is
#!/usr/bin/env python
print("This is to help you find new bands!")
pop = 0
def indie():
global indie
global classic_rock
global metal
global pop
indie = 0
classic_rock = 0
metal = 0
pop = 0
indie += 3
classic_rock -= 1
metal -= 1.5
pop -= 3
def notindie():
global indie
indie += 1
def classicrock():
global classic_rock
classic_rock += 2
def notclassicrock():
global classic_rock
classic_rock -= 1
def popp():
global pop
global indie
pop += 3
indie -= 3
def notpop():
global pop
global metal
pop -= 1
metal += 1
def notmetal():
global metal
global pop
metal -= 3
pop += 1
def metal():
global metal
global pop
global indie
global classicrock
classicrock += 1
metal += 3
indie -= 1
pop -= 4
que = input("Do you consider yourself to be a hipster? (Yes/No) ")
if que == 'yes':
indie()
if que == 'no':
notindie()
que2 = input("Do you like the Rolling Stones? (Yes/No) ")
if que2 == 'yes':
classicrock()
if que2 == 'no':
notclassicrock()
que3 = input("Do you like Britney Spears? (Yes/No) ")
if que3 == 'yes':
popp()
if que3 == 'no':
notpop()
que4 = input("Do you like Metallica? (Yes/No) ")
if que4 == 'no':
notmetal()
if que4 == 'yes':
metal()
if i enter yes for do you like metallica, i get the error
File "tastepy.py", line 69, in <module>
metal()
TypeError: 'float' object is not callable
if i enter in no for the hipster question:
Traceback (most recent call last):
File "tastepy.py", line 54, in <module>
notindie()
File "tastepy.py", line 19, in notindie
indie += 1
TypeError: unsupported operand type(s) for +=: 'function' and 'int'
I get these although there is no float anything in metal() anybody know what is going on?
-
1I recommend you read up about dicts and lists. Your implementation is about as graceful as a turtle in a tutu.Joel Cornett– Joel Cornett2012年02月13日 01:54:44 +00:00Commented Feb 13, 2012 at 1:54
-
whaddaya mean, implementation...Billjk– Billjk2012年02月13日 03:16:47 +00:00Commented Feb 13, 2012 at 3:16
-
I mean that you're using 70 lines of code to write something that shouldn't take more than 20. You're not taking advantage of the structure of the python language. I would recommend examining other people's code, and reading the python language tutorials at python.orgJoel Cornett– Joel Cornett2012年02月13日 04:50:36 +00:00Commented Feb 13, 2012 at 4:50
2 Answers 2
The problem is that you are using the same names for your functions as your variables, and they are clobbering each other. Try using different names, e.g. for functions likes_metal() and for the variable metal_score.
Also you should declare and initialize your globals at the global level, not within the indie function.
4 Comments
pop = 0 in the 2nd line. As @Joel said, globals are not great form, but you're already using them (or trying to), so this is how you do it properly.you are using the same names for your functions and your global variables. consequently when you run any function you delete all the functions and replace them with ints or floats