2010-03-21 68 views
3

我收到的Python誤以爲浮動字符串

TypeError: Can't convert 'float' object to str implicitly 

同時使用

Gambler.pot += round(self.bet + self.money * 0.1) 

哪裏鍋,賭,和金錢都是雙打(或至少應該是)。我不確定這是不是另一個Eclipse的東西,但我如何獲得編譯的線?

其中betmoney被初始化代碼:

class Gambler: 
    money = 0 
    bet = 0 

測試用例:

number = 0 
print("Here, the number is a {0}".format(type(number))) 
number = input("Enter in something: ") 
print("But now, it has turned into a {0}".format(type(number))) 

從測試情況下的輸出:

Here, the number is a <class 'int'> 
Enter in something: 1 
But now, it has turned into a <class 'str'> 

顯然,輸入()被其更改爲一個字符串。

EDIT:與

self.bet = int(self.bet.strip()) 

最後固定的問題(我認爲)用戶輸入值之後。雖然我不知道,如果這對解決問題:)

由丹尼爾·摹:更好的解決方案

self.bet = float(input("How much would you like to bet? $")) 
+1

請顯示self.bet和self.money初始化的代碼。 – GreenMatt 2010-03-21 01:47:46

+1

你還沒有證明這些不是字符串。 – 2010-03-21 01:48:14

+1

這就夠了,還是我應該發佈更多的代碼? – wrongusername 2010-03-21 01:48:47

回答

3

Python3.2 (py3k:77602)給出了這些錯誤信息:

 
>>> "1.2" * 0.1            #1 
Traceback (most recent call last): 
    File "", line 1, in 
TypeError: can't multiply sequence by non-int of type 'float' 
>>> "3.4" + 1.2 * 0.1           #2 
Traceback (most recent call last): 
    File "", line 1, in 
TypeError: Can't convert 'float' object to str implicitly 
>>> n = "42" 
>>> n += round(3.4 + 1.2 * 0.1)        #3 
Traceback (most recent call last): 
    File "", line 1, in 
TypeError: Can't convert 'int' object to str implicitly 

我懷疑你的錯誤消息,是因爲你的實際值是字符串,而不是預期漂浮在類似的場景到#2,這與您的例外完全匹配。

如果你可以寫一個testcase,這將是一個很大的幫助。


記住Py3.x的輸入是相同的Py2.x的原料 _Input和Py2.x的輸入消失(這相當於使用evai,你不要」不想做)。因此,3.x中的輸入將始終返回一個字符串。使用INT轉換:

n = int(input("Enter a number: ")) 

如果你想處理輸入錯誤,再搭上ValueError異常,這是什麼INT提出的錯誤:

try: 
    n = int(input("Enter a number: ")) 
except ValueError: 
    print("invalid input") 
else: 
    print("squared:", n*n) 
+1

我使用3.1.1。我會盡快寫測試用例如果我可以:) – wrongusername 2010-03-21 01:58:11

+1

我做到了......問題似乎在於輸入內容將其變爲字符串 – wrongusername 2010-03-21 02:11:04

+1

感謝您添加額外的信息! – wrongusername 2010-03-21 02:29:37

3

如果任何Gambler.potself.betself.money已儼然成爲字符串(因爲他們設定的最佳方式到某個點的字符串),+將被視爲意味着字符串連接,這會導致您看到的錯誤消息。

+1

它似乎self.bet有輸入後:( – wrongusername 2010-03-21 02:12:57

0

正如在評論說,你已經顯示的初始化局部變量爲0.而是嘗試類似於:

class Gambler: 
    def __init__(self): 
     self.bet = 0.0 
     self.money = 0.0 

    def calc_pot(self): 
     self.pot = round(self.bet + self.money * 0.1) 

g = Gambler() 
g.bet = 2.0 
g.money = 5.0 
g.calc_pot() 

print "Pot = %f" % (g.pot) 

另外,make sur沒有什麼東西可以把這些成員變成字符串。

6

input()在3.x中只返回字符串。程序員的工作是將其傳遞給一個數字構造函數,以便將其變成一個數字。

+1

是的......謝謝! – wrongusername 2010-03-21 02:17:13

+2

我相信在3.x中的input()與在2.x中的raw_input()相同 – MatrixFrog 2010-03-21 02:17:15

+1

@MatrixFogg:當我嘗試這樣做時,我得到'NameError:全局名'raw_input'沒有被定義'。難道我做錯了什麼? – wrongusername 2010-03-21 02:20:31

2

在Python 3.x中,input()取代了Python 2.x的raw_input()。因此,函數input()返回用戶輸入的確切字符串(如raw_input()在先前版本中所做的那樣)。

要獲得的Python 2.x的行爲,你可以做

number = eval(input("Please enter a number: ")) 

不過,我不會建議使用「EVAL」,因爲用戶可以把他們想要的Python有任何線,這是可能不是你想要的。如果你知道你想要一個float,只是告訴Python的這就是你想要的東西:

number = float(input("Please enter a number: ")) 
+1

謝謝!這也適用於我! – wrongusername 2010-03-21 02:22:18

4

你初始化鍋?您是否嘗試過存儲中間結果來追蹤問題來自哪裏?最後,你知道pdb嗎?這可能是一個很大的幫助。

class Gambler: 
    pot = 0.0 
    def __init__(self, money=0.0) 
     self.pot = 0.0 
     self.bet = 0.0 
     self.money = money 

    def update_pot(self): 
     import pdb; pdb.set_trace() 
     to_pot = self.bet + self.money * 0.1 
     to_pot = round(to_pot) 
     Gambler.pot = Gambler.pot + to_pot 

當執行set_trace()行時,您將收到提示。當你到達那裏時,試着看看當前的值。

(Pdb) h # help 
(Pdb) n # go to next statement 
(Pdb) l # list source code 
... 
(Pdb) to_pot 
... 
(Pdb) self.bet 
... 
(Pdb) self.money 
... 
(Pdb) Gambler.pot 
... 
(Pdb) c # continue 
+3

+1。調試器未被充分利用。 – 2010-03-21 02:22:47

+1

+1不知道調試器 – wrongusername 2010-03-21 02:25:58