简体   繁体   English

该代码未显示正确答案

[英]This code isn't displaying the correct answer

dictionary = open('dictionary.txt','r')
def main():
    print("part 4")
    part4()
def part4():
    naclcount = 0
    for words in dictionary:
        if 'nacl' in words:
            naclcount = naclcount + 1
    return naclcount
main()

basically it comes with the answer 25 which is right, except when I put another function in before part 4, it will print as 0. 基本上它带有正确的答案25,除了当我在第4部分之前放置另一个函数时,它将打印为0。

def part1():
    vowels = 'aeiouy'
    vowelcount = 0
    for words in dictionary:
        words = words.lower()
        vowelcount = 0
        if len(words) == 8:
            if 's' not in words:
                for letters in words:
                    if letters in vowels:
                        vowelcount += 1
                if vowelcount == 1:
                    print(words)
    return words

You should probably pass the filename as an argument to the part4 function, then create a new file object, because when you iterate through it once, it will stop returning new lines. 您可能应该将文件名作为参数传递给part4函数,然后创建一个新的文件对象,因为当您对其进行一次迭代时,它将停止返回新行。

def main():
    dict_filename = 'dictionary.txt'
    print("part 4")
    part4(dict_filename)


def part1(dict_filename):
    vowels = 'aeiouy'
    vowelcount = 0
    dictionary = open(dict_filename,'r')
    for words in dictionary:
        words = words.lower()
        vowelcount = 0
        if len(words) == 8:
            if 's' not in words:
                for letters in words:
                    if letters in vowels:
                        vowelcount += 1
                if vowelcount == 1:
                    print(words)
    return words

def part4(dict_filename):
    dictionary = open(dict_filename,'r')
    naclcount = 0
    for words in dictionary:
        if 'nacl' in words:
            naclcount = naclcount + 1
    return naclcount

main()

Also, if you want to be able to use your script either as an imported module or standalone, you should use 另外,如果您希望能够将脚本作为导入模块或独立使用,则应使用

if __name__ == '__main__':
    dict_filename = 'dictionary.txt'
    print("part 4")
    part4(dict_filename)

in place of the main function 代替main功能

Can you please paste your another function. 您能否粘贴您的另一个功能。 The problem is unclear unless you show your another function. 除非您显示其他功能,否则问题尚不清楚。

From what I inferred by your question and code. 根据您的问题和代码推断出的结果。 I have modified (clean) your code. 我已修改(清除)您的代码。 See if it helps. 看看是否有帮助。

def part4(dictionary):
    words = dictionary.readlines()[0].split(' ')
    nacl_count = 0
    for word in words:
        if word == 'the':
            nacl_count += 1
        #print nacl_count
    return nacl_count


def part1(dictionary):
    words = dictionary.readlines()[0].split(' ')
    words = [word.lower() for word in words]
    vowels = list('aeiou')
    vowel_count = 0
    for word in words:
        if len(word) == 8 and 's' not in word:
            for letter in words:
                if letter in vowels:
                    vowel_count += 1
        if vowel_count == 1:
            #print(word)
            return word


def main():
    dictionary = open('./dictionary.txt', 'r')
    print("part 1")
    #part1(dictionary)
    print("part 4")
    part4(dictionary)


main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM