我觉得问这个问题真的很傻,但是我已经为如何改进我的代码而苦苦挣扎了几天,我无法辨别出改进实际设计的明显方法,尽管它被认为很简单,但它效率低下且丑陋......
我需要使用关键事件在几个列表中移动。
为简单起见,假设第一个列表是“商店”,第二个是“产品”</p>
店铺 = [ShopA,ShopB,ShopC,ShopcD,ShopE]
产品 = [橙子、牛奶、水、巧克力、大米]
假设使用键盘键“1”和“2”,我想分别沿着“商店”列表向前和向后移动。另一方面,我想使用“3”和“4”键分别在“产品”列表上向前和向后移动。
现在,我的方法包含几个计数器:“ShopCounter”和“ProductsCounter”。顺便说一句,每次我沿着“Shops”列表移动时,我都会将“ProductsCounter”设置为零,这样它就可以从第一个项目开始检查(在这个例子中是橙子:)
我的问题现在在于柜台的位置/设计。我在这段代码中有四个部分:一个用于我想要输入的每个关键指令(下一个/上一个商店;下一个/上一个产品)。
由于 python 从零开始列表,显而易见的原因是将计数器放在每个代码段的末尾。但是,如果我这样做,如果上一个命令是前进/后退,我必须按每个键两次后退/前进键。因为它保留了前一个命令中增加的计数器
如果我将计数器放在每个部分的开头(这使每个部分更......更容易理解......)我必须设置一个“if”检查以确保代码不会超过列表中的第一项. 同样遵循这个设计,我必须检查一个额外的 if 检查以确保它不会尝试找到超出列表长度的项目......
有关处理此类操作的正确方法的任何建议?这将是最受欢迎的。
编辑:
此代码代表我提供的示例,其中包含我想要更改的结构。它有效,我只是认为应该有一种更合乎逻辑的方式来做到这一点。
import matplotlib.pyplot as plt
def CounterManager(Key, ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter):
if Key == "1" or Key == "2":
ProductsCounter = 0
if ShopsCounter == 0 and ShopsStarterCheck == True:
ShopsStarterCheck = False
print "eo"
elif Key == "1" :
ShopsCounter = ShopsCounter + 1
elif Key == "2" :
ShopsCounter = ShopsCounter - 1
if ShopsCounter <= 0:
ShopsCounter = 0
if ShopsCounter >= ShopNumber - 1:
ShopsCounter = ShopNumber - 1
ProductsStarterCheck = True
print "--Shop: " + str(ShopsCounter)
if Key == "3" or Key == "4":
if ProductsCounter == 0 and ProductsStarterCheck == True:
ProductsStarterCheck = False
elif Key == "3" :
ProductsCounter = ProductsCounter + 1
elif Key == "4" :
ProductsCounter = ProductsCounter - 1
if ProductsCounter <= 0:
ProductsCounter = 0
if ProductsCounter >= ProductsNumber - 1:
ProductsCounter = ProductsNumber - 1
print "--Product: " + str(ProductsCounter)
return ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter
def Key_Manager(event):
global ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter
if event.key == '1' or event.key == '2':
ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter = CounterManager(event.key, ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter)
print Shops[ShopsCounter]
if event.key == '3' or event.key == '4':
ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter = CounterManager(event.key, ShopNumber, ProductsNumber, ShopsStarterCheck, ProductsStarterCheck, ShopsCounter, ProductsCounter)
print Products[ProductsCounter]
Shops = ["ShopA","ShopB","ShopC","ShopD","ShopE"]
Products = ["Oranges", "Milk", "Water", "Chocolate", "Rice"]
ShopNumber, ProductsNumber = len(Shops), len(Products)
ShopsStarterCheck,ProductsStarterCheck = True,True
ShopsCounter,ProductsCounter, = 0,0
Fig1=plt.figure(figsize=(10,5))
Axis1 = Fig1.add_subplot(111)
Fig_Connection = {}
Fig_Connection['cid'] = plt.gcf().canvas.mpl_connect('key_press_event',Key_Manager) #Create figure an connect the event to it
plt.show()