需要帮助修复python错误

时间:2015-04-26 21:51:17

标签: python

这是我正在制作的编程语言的basic.py文件。目前它正在抛出一个错误。

STRING "HELLO WORLD"
string "Hey world!"
17 + 3 

这是我传递数据的test.vil文件(我的编程语言叫做villar):

IndexError: List index out of range

结果,我在第62行得到/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package computercompany; /** * * @author obliv_000 */ public class StockItem { private String itemID; private String itemDesc; private double price; private int quantity; private int reOrderLevel; public String getItemID() { return itemID; } public String getItemDesc() { return itemDesc; } public double getPrice() { return price; } public int getQuantity() { return quantity; } public int getReOrderLevel() { return reOrderLevel; } public StockItem(String itemsID, String itemsDesc, double p, int quant, int rol) { itemID = itemsID; itemDesc = itemsDesc; quantity = quant; reOrderLevel = rol; price = p; } public void setPrice(double price) { this.price = price; } public void setQuantity(int quantity) { this.quantity = quantity; } public void setReOrderLevel(int reOrderLevel) { this.reOrderLevel = reOrderLevel; } public String toString() { return "itemID = " + getItemID() + ", itemDesc = " + getItemDesc() + ", price = " + getPrice() + ", quantify = " + getQuantity() + ", reOrderLevel = " + getReOrderLevel(); } public String format() { return "The values for the following variables are:\nItemID: " + getItemID() + "\nItemDesc: " + getItemDesc() + "\nPrice: " + getPrice() + "\nQuantify: " + getQuantity() + "\nReOrderLevel: " + getReOrderLevel(); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package computercompany; /** * * @author obliv_000 */ public interface StockList { public StockLinkedList stock = new StockLinkedList(); public void addItem(StockItem item); public void deleteItem(String itemID); public void updateItemPrice(String itemID, double price); public void updateItemQuantity(String itemID, int quantity); public void updateReOrderLevel(String itemID, int reOrderLevel); public String formatStockList(String list) { list = String.format ("ItemID Description Price Qnty Re-Order Level\n" + "****** *********** ***** **** **************\n"); return list; } public String formatReOrderList() { ListIterator<StockItem> iterf = ItemList.listIterator(); String reReOrderList = FormattedTitle; while(iterf.hasNext()) { StockItem temp = iterf.next; int quant = temp.getQuantity(); int reorder = temp.getReOrderLevel(); if(quant<reorder) { formattedReOrder += iterf.next().format() + "\n"; } } } ListIterator listIterator() { throw new UnsupportedOperationException("Not yet implemented"); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package computercompany; /** * * @author obliv_000 */ public class StockLinkedList<StockList> { private StockLinkedList<StockList> prevItem; private StockItem myItem; private StockLinkedList<StockList> nextItem; public StockLinkedList(StockList item) { this(null, item, null); } public StockLinkedList(StockList item, StockLinkedList<StockList> previous, StockLinkedList next) { myItem = item; nextItem = next; prevItem = previous; } } public StockList getMyItem() { return myItem; } public StockLinkedList<StockList> getNextItem() { return nextItem; } public StockLinkedList<StockList> getPrevItem() { return prevItem; } public void setItem(StockList item) { myItem = item; } public void setPrevItem(StockLinkedList<StockList> previous) { prevItem = previous; } public void setNextItem(StockLinkedList<StockList> next) { nextItem = next; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package computercompany; import static computercompany.StockList.stock; /** * * @author obliv_000 */ public class StockListCLI { private StockList stock = null; public StockListCLI(StockList stock) { } // Displays main menu and gets valid option from user public void doMenu() { } // Obtain input for stock list operation // and invoke operation private void doAddItem() { stockList.add(item); } private void doDeleteItem() { Iterator<StockItem> itr = stockList.listIterator(); while(itr.hasNext()) { StockItem item = itr.next(); if(item.getItemID().equals(itemID)) { itr.remove(); break; } } } private void doUpdateItemPrice() { ListIterator itr = stock.listIterator(); while(itr.hasNext()) { StockItem item = (StockItem)itr.next(); if(item.getItemID().equals(itemID)) { item.setPrice(price); break; } } } private void doUpdateItemQuantity() { ListIterator itr = stock.listIterator(); while(itr.hasNext()) { StockItem item = (StockItem)itr.next(); if(item.getItemID().equals(itemID)) { item.setReOrderLevel(reOrderLevel); break; } } } private void doUpdateReOrderLevel() { StockLinkedList itr = stock.listIterator(); while(itr.setNextItem()) { StockItem item = (StockItem)itr.next(); if(item.getItemID().equals(itemID)) { item.setQuantity(quantity); break; } } } // Display contents of stock list private void doPrintStockList() { } // Display contents of re-order list private void doPrintReorderLIst() { } }

你能帮助我吗?如果允许的话,我会喜欢如何改进它的建议。

1 个答案:

答案 0 :(得分:3)

你已经有了这条线:

while(i < len(toks)):
parse函数中的

。但是,在此while循环中,您访问toks[i+1]元素,该元素将在while循环的最后一次迭代中超出范围(如i == len(toks)-1i+1 == len(toks)边界并抛出错误)。您需要将以上行更改为:

while(i < len(toks)-1):

以便最终迭代i == len(toks) - 2i+1 == len(toks) - 1都在边界内。

相关问题