【问题标题】:Assistance with function not working辅助功能不起作用
【发布时间】:2014-08-28 22:55:48
【问题描述】:

我的代码的show_list 函数不起作用。我收到一条消息,指出 'multiples' 未定义,但我无法确定问题。有人可以对我做错了什么进行审查和建议。

def main():
    input1 = int(input("enter the low integer: "))
    input2 = int(input("enter the high integer: "))
    input3 = int(input("enter the integer for the multiples: "))

    show_multiples(input1, input2, input3)

    print ("List was created")


 def show_multiples(input1, input2, input3):

    num_range = range(input2, input1, -1)
    multiples = []

    for num in num_range:
        if num % input3 == 0:
            multiples.append(num)
            return multiples


    show_list(multiples)

def show_list(multiples):

    elem = len(multiples)
    average = sum(multiples) / elem
    num_range = range(input2, input1, -1)

    print ("the list has", elem, "elements.")

    for num in num_range:
        if num % input3 == 0:
        print (num, end=" ")

    print ("Average of multiples is  ", average)



main()

【问题讨论】:

    标签: python list function sum return


    【解决方案1】:

    在定义函数show_list之前调用show_list(multiples)

    将你的 main 函数放在你的代码末尾并调用 main() 来运行它:

    def main():
    
        input1 = int(input("enter the low integer: "))
        input2 = int(input("enter the high integer: "))
        input3 = int(input("enter the integer for the multiples: "))
    
        show_multiples(input1, input2, input3)
    
    print ("List was created")
    main()
    

    仅调用 show_list(multiples) 将其移至定义 show_list 的下方

    不过你会遇到更多问题:

    def show_list(multiples):
        elem = len(multiples)
        average = elem / sum(multiples)
        print ("the list has", elem, "elements.")
        # num_range not defined only exists in show_multiples and input3 is also not accessable
        for num in num_range:
            if num % input3 == 0: 
                multiples.append(num)
                print (num)
    

    不完全确定你想要什么,但我想这会让你更接近:

    input1 = int(input("enter the low integer: "))
    input2 = int(input("enter the high integer: "))
    input3 = int(input("enter the integer for the multiples: "))
    num_range = range(input2, input1, -1)
    
    def show_multiples():
        multiples = []
        for num in num_range:
            if num % input3 == 0:
                multiples.append(num)
        return multiples
    
    def show_list():
        multiples = show_multiples()
        elem = len(multiples)
        average = elem / sum(multiples)
        print ("the list has", elem, "elements.")
        for num in num_range:
            if num % input3 == 0:
                multiples.append(num)
            print (num)
    
    show_list()
    

    【讨论】:

      【解决方案2】:

      mutliples 不在全局范围内定义,仅在 show_multiples() 范围内定义

      你可能想做的是在全局范围内,改变

      show_multiples(input1, input2, input3)
      

      multiples = show_multiples(input1, input2, input3)
      

      【讨论】:

        猜你喜欢
        • 2022-11-30
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        相关资源
        最近更新 更多