该部分功能的目的是从其他功能构建的字典中获取与某个日期相关的数据,并将它们排列到一个字典中。当dayId指向当前日期时,该部件工作正常。但是,当从HTML检索日期时,for循环中的一次迭代后,列表stallOperatingHour变为0。为什么会发生这种情况,我应该如何解决?
如果未提供来自HTML的输入,则dayId表示当前日期。
我添加了打印功能,以查看程序在遇到错误之前能走多远(下面列出的一些打印功能)。从HTML成功检索了输入,第一次迭代运行良好,并且我们从中创建列表的字典在第二次迭代中是正确的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Circular_path extends JFrame{
public Circular_path() {
setTitle("Circular_path");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Canvas canvas = new Canvas();
add(canvas);
setVisible(true);
}
protected class Canvas extends JComponent implements actionListener{
protected double a=getWidth()/2,b=getHeight()/2; //the center of the orbit
protected double x= 0,y = 0; //initialising x and y coordinates
protected int r=100; //radius of circle
protected double theta = 0.0;//angle theta
public Canvas() {
timer.start();
}
//create Timer object
Timer timer = new Timer(10,this);
@Override
public void paintComponent(Graphics g) {
System.out.println("Canvas.paintComponent");
g.setColor(Color.RED);
g.fillRect((int)x,(int)y,10,10);
}
public void actionPerformed(actionEvent e) {
x = a+ (r*Math.cos(theta));
y = b+ (r*Math.sin(theta));
repaint();
theta= theta+(Math.PI/1000);
}
}
public static void main(String[] args) {
new Circular_path();
}
}
以下部分是给定的输出。
def getavailableStalls():
stallsDict = readStall()
stallMenuByDayDict = readStallMenuByDay()
stallOperatingHourDict = readStallOperatingHour()
allDaysDict = readDay()
menuItemsDict = readMenuItems()
try:
dayId,currentHour,dt_string = filterByTime() #filterByTime is the function retrieving input and processing them
except:
dayId,currentHour = -1,-1
if dayId == -1:
currentDT = datetime.now()
currentDay = currentDT.strftime('%A')
currentHour = int(currentDT.strftime('%H'))
dayId = list(k for k,v in allDaysDict.items() if v == currentDay)[0]
dt_string = datetime.now().strftime("%A,%x %H:%M:%S")
currentAvailableStallsDict = {}
unavailableStalls = 0
# Get stalls by current DT
for stallId,stallDetails in sorted(stallsDict.items()):
# Get stall operating hour
# Get Stall Operating Hours on Weekends
if(dayId == "6" or dayId == "7"):
stallOperatingHour = list(v for k,v in stallOperatingHourDict if v[0] == stallId and v[1] == dayId)
startOperatingHour = stallOperatingHour[0][2]
endOperatingHour = stallOperatingHour[0][3]
else:
# Get Stall Operating Hours on Weekdays
stallOperatingHour = list(v for k,v in stallOperatingHourDict if v[0] == stallId and (v[1] == ''))
listTemp = list(v for k,v in stallOperatingHourDict if v[0] == stallId and v[4] == "0" and v[1] == dayId)
if(len(listTemp) != 0):
stallOperatingHour.insert(1,listTemp[0])
print("before start operating hour")
print(stallOperatingHourDict)
print(stallOperatingHour)
startOperatingHour = float(stallOperatingHour[0][2])
print("pass start operating hour")
endOperatingHour = float(stallOperatingHour[0][3])
print("pass end operating hour")
如果一切正常,则第二次迭代应产生stallOperatingHour = Tuesday,09/08/20 04:32:00 #correct input retrieved
before start operating hour #first iteration
dict_items([('1',('1','','7','0','1')),('2','6',('3','10.5','22',('4','1','19','0')),('5','2',('6','3','8','20',('7','4','10',('8','5','9','21',('9',('10',('11','11',('12','14',('13',('14',('15',('16',('17',('18',('19','1'))])
[('1','1')]
pass start operating hour
pass end operating hour
before start operating hour #second iteration
dict_items([('1','1'))])
[] #and it becomes empty
IndexError: list index out of range