Python accepting float for string

I get

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

using

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

where the bank, rate and money are all doubled (or at least should be). I'm not sure if this is another Eclipse thing, but how do I get the string to compile?

Code, where bet, and moneyare initialized:

class Gambler:
    money = 0
    bet = 0

Test case:

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)))

Exiting the test case:

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

Apparently input () changes it to a string.

EDIT : finally fixed the problem (I think) with

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

after the user enters a value. Although I do not know if this is the best way to fix the problem :)

Best Daniel G Solution:

self.bet = float(input("How much would you like to bet? $"))
+3
source share
6 answers

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, .

test case, .


, Py3.x Py2.x raw _input, Py2.x ( evai, ). - 3.x . int :

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

, ValueError, :

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

input() 3.x . , .

+6

? ? , , 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
+4

- Gambler.pot, self.bet self.money - ( - ), + , .

+3

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: "))
+2

, , , 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)

, , , .

0

Source: https://habr.com/ru/post/1737868/


All Articles