2012-02-13 59 views
0

我正在製作一個向您建議新音樂的python腳本,但由於某種原因,我收到了很多錯誤。劇本還沒有完成,但在這裏它是奇怪的Python錯誤

#!/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() 

,如果我是輸入你喜歡METALLICA,我得到的錯誤

File "tastepy.py", line 69, in <module> 
    metal() 
TypeError: 'float' object is not callable 

如果我不爲時髦的問題輸入:

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' 

我得到這些雖然沒有任何浮動金屬() 任何人都知道發生了什麼?

+1

我建議你閱讀[dicts](http://docs.python.org/tutorial/datastructures.html#dictionaries)和[表](http://docs.python.org/tutorial/introduction.html#lists)。你的實現和芭蕾舞蹈中的烏龜一樣優雅。 – 2012-02-13 01:54:44

+0

whaddaya的意思是,執行... – Billjk 2012-02-13 03:16:47

+0

我的意思是說,你使用70行代碼來寫一些不應該超過20的東西。你沒有利用python語言的結構。我會推薦檢查其他人的代碼,並在[python.org](http://docs.python.org/tutorial/) – 2012-02-13 04:50:36

回答

5

問題是,你的函數使用了和你的變量相同的名字,並且它們是互相破壞的。嘗試使用不同的名稱,例如功能likes_metal()和變量metal_score

此外,您應該聲明並初始化全局級別的全局變量,而不是在indie函數中。

+0

上閱讀python語言教程,非常感謝! – Billjk 2012-02-13 01:16:15

+0

你是指在全球範圍內,你認爲的功能之外的意思是什麼? – Billjk 2012-02-13 01:24:58

+1

也不建議您首先使用全局變量。最好在函數參數中傳遞值。 – 2012-02-13 01:59:09

1

您正在爲您的函數和全局變量使用相同的名稱。因此當你運行任何函數時,你刪除了所有的函數,並用int或者float替換它們。