在 python 中添加 'int' 和 'str',我应该更改什么来修复它
Adding 'int' and 'str' in python, What should I change to fix it
class cards(object):
suit_dict = {
"d" : "Diamonds" , "h" : "Hearts" , "c" : "clubs" , "s" : "Spades"
}
values_dict = { 1: 'Ace',
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 'Jack',
12: 'Queen',
13: 'King'
}
def __init__(self,values,suits):
suits = suits.lower
self.values = values
self.suits = suits
def __str__(self):
my_card = str(self.values_dict[self.values]) + "of" + self.suit_dict[self.suits]
return my_card
his_card = cards(2, "D")
print(his_card)
好像我在添加字符串和整数时遇到错误;我不确定要更改什么才能使其正常工作。
my_card = self.values_dict[self.values] + "of" + self.suit_dict[self.suits]
TypeError: unsupported operand type(s) for +: 'int' and 'str'
我尝试制作 str(self.values_dict[self.values]),但现在我得到了不同类型的错误。
my_card = str(self.values_dict[self.values]) + "of" + self.suit_dict[self.suits]
KeyError: <built-in method lower of str object at 0x000001536717F830>
只要在你的 lower 方法中添加括号就可以让代码为我工作。
如下:
def __init__(self,values,suits):
self.values = values
self.suits = suits.lower()
class cards(object):
suit_dict = {
"d" : "Diamonds" , "h" : "Hearts" , "c" : "clubs" , "s" : "Spades"
}
values_dict = { 1: 'Ace',
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 'Jack',
12: 'Queen',
13: 'King'
}
def __init__(self,values,suits):
suits = suits.lower
self.values = values
self.suits = suits
def __str__(self):
my_card = str(self.values_dict[self.values]) + "of" + self.suit_dict[self.suits]
return my_card
his_card = cards(2, "D")
print(his_card)
好像我在添加字符串和整数时遇到错误;我不确定要更改什么才能使其正常工作。
my_card = self.values_dict[self.values] + "of" + self.suit_dict[self.suits]
TypeError: unsupported operand type(s) for +: 'int' and 'str'
我尝试制作 str(self.values_dict[self.values]),但现在我得到了不同类型的错误。
my_card = str(self.values_dict[self.values]) + "of" + self.suit_dict[self.suits]
KeyError: <built-in method lower of str object at 0x000001536717F830>
只要在你的 lower 方法中添加括号就可以让代码为我工作。
如下:
def __init__(self,values,suits):
self.values = values
self.suits = suits.lower()