2012-12-22 53 views
1

我目前正在寫一個貿易遊戲,用戶連接到服務器,然後彼此貿易和賺錢等 但是當我嘗試Python的「海峽」對象不是可調用的Python 2.7.3

if(input.lower() == 'sell'): 
     sMaterial = raw_input('Material: ') 
     if(sMaterial.lower() == 'gold'): 
      sAmount = int(input('Enter amount: ')) 
      if(gold >= sAmount): 
       mon = mon + (100 * sAmount) 
      else: 
       print 'You do not have enough', sMaterial 

它引發錯誤

> sell 
Material: gold 
Traceback (most recent call last): 
    File "Test.py", line 119, in <module> 
    sAmount = int(input('Enter amount: ')) 
TypeError: 'str' object is not callable 
我使用Linux,Python版本2.7.3

,與Geany開發環境。 在此先感謝。

回答

9

這條線:

if(input.lower() == 'sell'): 

告訴我,你必須綁定的名稱input在某個時刻的字符串。所以,當你調用

sAmount = int(input('Enter amount: ')) 

你試圖論證'Enter amount: '傳遞給input,因此:TypeError: 'str' object is not callable。由於它看起來像使用Python 2,因此無論如何您應該可以使用raw_input,但這是不重新綁定內置名稱的另一個原因。

1

你應該做的

sAmount = int(raw_input('Enter amount: ')) 

,而不是

sAmount = int(input('Enter amount: ')) 

,你可能想要做一些例外處理的地方有太多:)

+0

謝謝,這固定它。我也看到了其他的帖子,他們幫助。對不起,我沒有注意到這一點。 – user1815591

2

已覆蓋input功能用變量持有一些數據。某處你做了input = ...。 (你可以在你的代碼的第一行看到你正在做input.lower()。)解決方法是更改​​你的代碼的一部分。不要給你的變量賦與內建函數或類型相同的名字。

相關問題