函数返回不正确的False(仅在更高的值时)

时间:2016-05-26 21:24:58

标签: python limits

在输入值较高时,即使输入是合法值,我的checkInputFile()函数也会返回false。这仅仅是值大于400的问题,但我希望能够使用最多10,000的数字。

我附上了用于获取用户输入的模块,以及用于产生侮辱的文本文件。

import random
from getNum import *

def main() :
    random.seed()
    allWords = loadInsults()
    print("One insult: ", end="")
    print(generateInsult(allWords))
    insults = generateInsults(allWords)
    displaySomeInsults(insults)
    saveInsults(insults)
    if checkInsultsFile() :
        print("\n1000 insults properly saved. They are unique and in order.")
    else :
        print("\nThe insults are not properly generated or saved!")

# Using all possible arguments and prompting the user for the number of insults.
    allWords = loadInsults("word1.txt", "word2.txt", "word3.txt")
    numInsults = getNumInsults()
    insults = generateInsults(allWords, numInsults)
    displaySomeInsults(insults)
    saveInsults(insults, "Insults.txt")
    if checkInsultsFile(numInsults, "Insults.txt") :
        print("\n" + str(numInsults) + " insults properly saved. They are unique and in order.")
    else :
        print("\nThe insults are not properly generated or saved!")

# Write your functions here:

def readFiles(fileName):
    inFile = open(fileName, "r")
    fileContentsList = []
    for line in inFile:
        fileContentsList.append(line.rstrip())
    inFile.close()
    return fileContentsList

def loadInsults(word1="word1.txt", word2="word2.txt", word3="word3.txt"):
    return readFiles(word1), readFiles(word2), readFiles(word3)

def generateInsult(allWords):
    return "thou " + random.choice(allWords[0]) + " " + random.choice(allWords[1]) \
           + " " + random.choice(allWords[2]) + "!"

def generateInsults(allWords, numInsults=100):
    insultList = []
    while len(insultList) < numInsults:

        insultList.append(generateInsult(allWords))
    insults = list(set(insultList))
    return insults

def displaySomeInsults(insults):
    insults.sort()
    print("Some insults:")
    for i in insults[0 : 10]:
        print(i)
    print(". \n<snip>\n.")
    for i in insults[-11 : -1]:
        print(i)

def saveInsults(insults, filename="Insults.txt"):
    if 100 <= len(insults) <= 10000:
        outFile = open(filename, "w")
        for line in insults:
            outFile.write(line + "\n")
        outFile.close()

def checkInsultsFile(numInsults=100, filename="Insults.txt"):
        inFile = open(filename, "r")
        newList = []
        for line in inFile:
            newList.append(line.rstrip())
        inFile.close()
        for i in range(1, len(newList)):
            if newList[i] < newList[i-1]:
                print("Not all insults are in order")
                return False
            elif newList[i] == newList[i-1]:
                print("Not all insults are unique")
                return False
        if len(newList) != numInsults:
            print("Wrong number of insults.")
            return False
        return True

def getNumInsults():
    numInsults = getNum("\nEnter a number between 100 and 10,000: ", 100, 10000)
    return numInsults

main()

1 个答案:

答案 0 :(得分:0)

我想我可能知道发生了什么,你在列表中创建了一组侮辱,这将删除任何重复。你产生的侮辱越多,重复的可能性就越大,这会导致你的侮辱小于numInsults。

此行将删除重复项:insults = list(set(insultList))

使侮辱名单的长度小于numInsults。