Title: Why does python come up with a TypeError even though I converted to strings?
Tags: python
Question: So I created a Player and Dealer class for my blackjack game, but when I put the integer I want to print in an f-string, it comes up with a TypeError:
in results
print(f&quot;{name}'s hand is {Player.hand}, total is {sum(Player.hand_val)}&quot;)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
It's really confusing because I clearly printed it as a string. I also tried .format(), and str(), but they all showed up the same error message.
class Player:
wallet = 5000
hand_val = []
hand = []
def __init__(self, wallet, hand_val=None, hand=None):
self.wallet = wallet
self.hand_val = hand_val
self.hand = hand
class Dealer(Player):
hand_val = []
hand = []
def __init__(self, hand_val, hand, wallet):
super().__init__(hand_val, hand)
self.hand_val = hand_val
self.hand = hand
def cardgenerate():
cards = [(&quot;A&quot;, 1), (&quot;2&quot;, 2), (&quot;3&quot;, 3), (&quot;4&quot;, 4), (&quot;5&quot;, 5), (&quot;6&quot;, 6), (&quot;7&quot;, 7), (&quot;8&quot;, 8), (&quot;9&quot;, 9), (&quot;10&quot;, 10),
(&quot;J&quot;, 10), (&quot;Q&quot;, 10), (&quot;K&quot;, 10)]
select = r.choice(cards)
cardGen.append(select[0])
cardGen_value.append(select[1])
cardGen = []
cardGen_value = []
def hit():
for i in range(2):
cardgenerate()
# playerHand is from even indexes from cardGen[]
Player.hand.append(cardGen[0])
Player.hand_val.append(cardGen[0])
# dealerHand is from odd indexes from cardGen[]
Dealer.hand.append(cardGen[1])
Dealer.hand_val.append(cardGen[1])
def stay():
cardgenerate()
Dealer.hand.append(cardGen[0])
Dealer.hand_val.append(cardGen[0])
def pcheck_lose():
if sum(Player.hand_val) &gt; 21:
print(&quot;BUST!!! You lost&quot;)
Player.hand_val = []
return True
def dcheck_lose():
if sum(cardGen_value) &gt; 21:
print(&quot;THE DEALER BUSTED!!! You won&quot;)
Dealer.hand_val = []
return True
def results():
print(f&quot;{name}'s hand is {Player.hand}, total is {sum(Player.hand_val)}&quot;)
print(f&quot;Dealer's hand is {Dealer.hand}, total is {sum(Dealer.hand_val)}&quot;)
Comment: I'm guessing `hand_val` is a list of strings? you need to add the code for that part.

Here is the accepted answer: I think this is the problem:
change this:
Player.hand_val.append(cardGen[0])
Player.hand_val.append(cardGen_value[0])
Comment for this answer: wow it worked lmao I feel so stupid... thanks : )

Here is another answer: You need to be sure that hand_val list has only integer/float values, somewhere maybe during initialization you are providing it a string?
hand_val = ['30', 30]
print(sum(hand_val))
gives unsupported operand type(s) for +: 'int' and 'str'
hand_val = [30, 30]
print(sum(hand_val))
gives output of 60.
If you still want to provide string to the table, and then sum it up, you can cast value to integer type by using int(<str_variable&gt;)
like so:
strg = '30'
print(type(int(strg)))
Output:
<class 'int'&gt;
Comment for this answer: Please provide full code of initialization of object and using the function then.
Comment for this answer: You could just edit original post.
Comment for this answer: Nope... I printed out Player.hand_val and it showed up a list of integers, not strings
Comment for this answer: def cardgenerate():
cards = [("A", 1), ("2", 2), ("3", 3), ("4", 4), ("5", 5), ("6", 6), ("7", 7), ("8", 8), ("9", 9), ("10", 10),
("J", 10), ("Q", 10), ("K", 10)]
select = r.choice(cards)
cardGen.append(select[0])
cardGen_value.append(select[1])
cardGen = []
cardGen_value = []
def hit():
for i in range(2):
cardgenerate()
# playerHand is from even indexes from cardGen[]
Player.hand.append(cardGen[0])
Player.hand_val.append(cardGen[0])
Dealer.hand.append(cardGen[1])
Dealer.hand_val.append(cardGen[1])
Comment for this answer: def stay():
cardgenerate()
Dealer.hand.append(cardGen[0])
Dealer.hand_val.append(cardGen[0])
def pcheck_lose():
if sum(Player.hand_val) > 21:
print("BUST!!! You lost")
Player.hand_val = []
return True
def dcheck_lose():
if sum(cardGen_value) > 21:
print("THE DEALER BUSTED!!! You won")
Dealer.hand_val = []
return True
def results():
print(f"{name}'s hand is {Player.hand}, total is {sum(Player.hand_val)}")
print(f"Dealer's hand is {Dealer.hand}, total is {sum(Dealer.hand_val)}")
Comment for this answer: RIP readability
Comment for this answer: yeah i realised sorry
Comment for this answer: its done, i edited it

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:09/01/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python>', 'Title': 'Why does python come up with a TypeError even though I converted to strings?', 'ViewCount': 38, 'id': 73565216, 'user_AboutMe': None, 'user_AccountId': 26188080.0, 'user_DisplayName': 'keqae', 'user_Reputation': 15.0, 'user_Views': 2.0, 'user_id': 19870341.0}}