While循环有时返回未定义

时间:2019-01-31 12:26:04

标签: javascript loops while-loop

我正在建立一个网站,您在其中放置一天中的某个时间,它会在整本圣经中搜索带有该参考文献的经文。例如,如果我输入5:12,它将搜索圣经的随机书,直到找到引用为5:12的经文。例如:诗篇5:12。或者至少这是应该如何工作的。我有一个循环,应该查找该节经文是否在特定书中,如果没有,则继续到下一本书。该循环不起作用。

    from PyQt5 import QtGui
    from PyQt5.QtWidgets import QApplication, QMainWindow,QAction
    from PyQt5.QtGui import QIcon
    import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.title = "PyQt5 Tool Bars"
        self.top = 100
        self.left = 100
        self.width = 680
        self.height = 500

        self.InitWindow()

    def InitWindow(self):
        #exitAct = QAction(QIcon('exit.png'), ' GoOut', self)
        #exitAct.setShortcut('Ctrl+Q')
        #exitAct.triggered.connect(self.CloseApp)

        exitAct = QAction(QIcon('exit.png'), '&Exit', self)
        print(exitAct.menuRole())  # It prints "1".
        QAction::TextHeuristicRole
        exitAct.setMenuRole(QAction.NoRole)

        copyAct = QAction(QIcon('copy.png'), 'Copy', self)
        copyAct.setShortcut('Ctrl+C')

        pasteAct = QAction(QIcon('paste.png'), 'Paste', self)
        pasteAct.setShortcut('Ctrl+V')

        deleteAct = QAction(QIcon('del.png'), 'Delete', self)
        deleteAct.setShortcut('Ctrl+D')

        saveAct = QAction(QIcon('save.png'), 'Save', self)
        saveAct.setShortcut('Ctrl+S')



        self.toolbar = self.addToolBar('Toolbar')

        self.toolbar.addAction(exitAct)
        self.toolbar.addAction(copyAct)
        self.toolbar.addAction(pasteAct)
        self.toolbar.addAction(deleteAct)
        self.toolbar.addAction(saveAct)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.show()


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

如果“创世纪”的参考编号为5:12,则效果很好,但如果“詹姆斯”的参考编号为5:12,则它自动求助于“无法读取未定义的属性”错误,或者输出“抱歉,那段时间我们没有诗歌。”当圣经的书中没有这节经文时,那应该只是输出。 ex 17:135应该输出抱歉消息。大部分都在工作。我试图通过在“ i ++”下调用一本新书来解决此问题,但这并不能解决问题。

该网站实际上是在线的,因此,如果您要自己进行测试,可以访问http://calebdidthis.com/timeverse

编辑:

我进行了一些更改,并且不再出现错误,但是当它应该循环浏览书籍时,我确实收到了“抱歉消息”

bookList = Object.getOwnPropertyNames(esvJSON)

randBook = bookList[Object.keys(bookList)[Math.floor(Math.random() * 
66)]];
inputVerse = esvJSON[randBook][input1];

Object.size = function(obj) {
    var bookLength = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) bookLength++;
    }
    return bookLength;
};

// Get the size of an object
var bookLength = Object.size(inputVerse);

verseIndex = ["01","1", 
"2","3","4","5","6","7","8","9","10","11","12",]
var varBoolean = true;
var i = 0;;

while (varBoolean == true) {
    if (input1 > bookLength) {
        i++;
        randBook = bookList[Object.keys(bookList) . 
[Math.floor(Math.random() * 66)]];
    }
    else if (esvJSON[randBook][input1][input2] == undefined) {
        i++;
        randBook = bookList[Object.keys(bookList) . 
[Math.floor(Math.random() * 66)]];
    }
    else {
        bibleVerse = esvJSON[randBook][input1][input2];
        output = randBook + " " + input1 + ":" + input2 + " " + 
bibleVerse;
        varBoolean = false;
    }
    if (i > 66) {
        varBoolean = false;
        output = "Sorry, we have no verse for your time."
    }
}

1 个答案:

答案 0 :(得分:0)

// Shuffle an array
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i]; a[i] = a[j]; a[j] = x;
    }
    return a;
};

// This gives an array of keys in envJSON
bookList = Object.getOwnPropertyNames(esvJSON);

// Shuffle the array of book names as you want to go through all the books, but randomly
// This makes sure none of the books is missed while checking
var shuffledBookList = shuffle(bookList);

// Now just loop through all the books in shuffledBookList
output = null;
for (var i = 0; i < shuffledBookList.length; i++) {
    // Get the random book name from array
    var randomBookName = shuffledBookList[i];

    // Get the actual book using bookname
    randBook = esvJSON[randomBookName];

    // Get the verse at input1 from randBook
    inputVerse = randBook[input1];

    // If the verse exists and verse has a value at input2 process it and break the loop
    if (inputVerse && inputVerse[input2]) {
        bibleVerse = inputVerse[input2];
        output = randomBookName + " " + input1 + ":" + input2 + " " + bibleVerse;
        break;
    }
}

// if output is still null, show the message
if (output === null) {
    output = "Sorry, we have no verse for your time.";
}

随机播放数组逻辑取自here