简化代码 def __draw_number(self): for item_l1 in self.map: for item in item_l1: v = item[0] if v <= 0: continue if item[2] > 0: continue self.screen.blit(self.number_map[f'{v}']['img'], [item[1][0] + 2, item[1][1] + 2])
时间: 2024-02-10 12:06:49 浏览: 8
可以简化为:
```
def __draw_number(self):
for item_l1 in self.map:
for item in item_l1:
v, pos, state = item
if v > 0 and state <= 0:
self.screen.blit(self.number_map[f'{v}']['img'], [pos[0] + 2, pos[1] + 2])
```
主要简化了 if 语句的判断条件,将两个条件合并为一个条件,增加了代码的可读性。同时,将 item[1] 和 item[2] 分别解包为 pos 和 state,使代码更加简洁。
相关问题
Fclass Container(object): def __init__(self): self.items = {} def getItem(self, uid): return self.items.get(uid) def addItem(self,item): self.items[item.uid] =item def removeItem(self, item): del self.items[item.uid] del item def __iter__(self): for item in self.items.values(: yield item
请注意,你提供的代码存在一些错误。在删除字典中的键值对时,你需要将 `del` 语句分开写,并且删除对象 `item` 的语法应为 `del item`。另外,在 `__iter__` 方法中,你遗漏了一个右括号 `)`。下面是已修正的代码:
```python
class Container(object):
def __init__(self):
self.items = {}
def getItem(self, uid):
return self.items.get(uid)
def addItem(self, item):
self.items[item.uid] = item
def removeItem(self, item):
del self.items[item.uid]
del item
def __iter__(self):
for item in self.items.values():
yield item
```
修正后的代码定义了一个名为 `Container` 的类,它具有以下方法:
- `__init__`:初始化一个空字典 `items`。
- `getItem`:根据给定的 `uid` 返回字典中对应的项。
- `addItem`:将给定的项添加到字典中,使用项的 `uid` 作为键。
- `removeItem`:从字典中移除给定的项,使用项的 `uid` 作为键。
- `__iter__`:迭代字典中的值,并使用生成器函数逐个返回。
这样,你就可以使用 `Container` 类来创建一个容器对象,并通过调用其方法来操作字典中的项。你可以迭代容器对象以获取其中的项,并使用其他方法来获取、添加和删除项。
#__iter__和__next__(迭代) class item(): def __init__(self,price,type): self.price=price self.type=type def __iter__(self): self.n=0 return self def __next__(self): if self.n<len(self.type): result=self.type[self.n] self.n += 1 else: raise StopIteration afruit=item(18.5,['apple','banana','pear','orange']) for types in afruit: print(types) 错误在哪
在你的代码中,`__next__`方法没有返回任何值,应该返回`result`。此外,`StopIteration`的引发应该在方法体内,而不是在方法名后面,因此应该在`if`语句块下面添加`raise StopIteration`。
下面是修改后的代码:
```python
class item():
def __init__(self, price, type):
self.price = price
self.type = type
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n < len(self.type):
result = self.type[self.n]
self.n += 1
return result
else:
raise StopIteration
afruit = item(18.5, ['apple', 'banana', 'pear', 'orange'])
for types in afruit:
print(types)
```
输出:
```
apple
banana
pear
orange
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)