检查给定号码的用户是否为幻数

时间:2019-07-10 05:55:55

标签: python python-3.x magic-numbers

对我来说

幻数:如果数字的总和变为1,直到结果的长度变为1,则称为幻数。

示例:1 [和:1],10 [和:1 + 0 = 1],91 [和:pass1:9 +1 = 10,pass2:1 + 0 = 1] ,100 [sum:1 + 0 + 0 = 1]这样。

问题是:我的代码可以正常输入:91,91的总和为:pass1:9 +1 = 10,pass2:1 + 0 =1。显示正确输出。但不适用于其他任何输入,例如:100、55等。

代码:

def Single_Digit_Summer(num):
    sum = 0
    while num > 0:
        sum += num % 10
        num = num // 10
    return Single_Digit_Summer(sum) if sum > 9 else sum
num15 = int(input("Enter number to check: "))
sum2 = Single_Digit_Summer(num15)
print(num15, "is a magic number.") if sum2 == 1 else print(num15, "is not a magic number.")

输出:

Case - 1:

Terminal: Enter number to check: 
Terminal input:91
Terminal output: 91 is a magic number.
Actual var 'sum' value: 1
Expected var 'sum' value: 1
Expected output: 91 is a magic number.
Decision: OK

Case - 2:

Terminal: Enter number to check: 
Terminal input:100
Terminal output: 100 is not a magic number.
Actual var 'sum' value: 0
Expected var 'sum' value: 1
Expected output: 100 is a magic number.
Decision: Wrong Output

Case - 3:

Terminal: Enter number to check:
Terminal input:55
Terminal output: 55 is not a magic number.
Actual var 'sum' value: 5
Expected var 'sum' value: 1
Expected output: 55 is a magic number.
Decision: Wrong Output

Case - 4:

Terminal: Enter number to check:
Terminal input: 1009
Terminal output: 1009 is not a magic number.
Actual var 'sum' value: 9
Expected var 'sum' value: 1
Expected output: 1009 is a magic number.
Decision: Wrong Output

要求: 我找不到为什么我的代码无法正常工作的原因。帮我调试。

0 个答案:

没有答案