首页 > 解决方案 > 为什么我的函数中出现错误?

问题描述

出于某种原因,计算的输出会multiply = func1_input * 9从输入中给出一个重复的数字,例如5555555,如果5输入了。对于sum of digits输出的计算也是您期望multiply给出的。

def function1():
    while True:
        try:
            func1_input = input("Please enter a number between 1 and 9: ")
            val = int(func1_input)
            if 1 <= val <= 9:
                print("You selected: ",func1_input)
                print("\n")
                function2(func1_input)
                break
            else:
                print("Invalid input. Please enter again.")
                function1()
        except ValueError:
            print("number not entered")

def function2(func1_input):

    func2_input = int(input("What is 10-1?: "))
    if func2_input == 9:
        print("Your number is: ",func2_input)
        print("\n")
        multiply = func1_input*9
        print("Your number multiplied by 9 is: ",multiply)
        function3(multiply)
    
    else:
        print("Invalid input. Please enter again.")
        function2(func2_input)

def function3(multiply):
    sum_of_digits = sum(int(digit) for digit in str(multiply))
    print("Adding up the individual digits of your number gives: ",sum_of_digits)
    function4(sum_of_digits)

标签: pythonmath

解决方案


您的错误在行中function2(func1_input)。应该是function2(val)


推荐阅读