繁体   English   中英

如何让我的 function 处理负数?

[英]How do I make my function work on negative numbers?

repeat = "y"
while repeat == "y":
    #First get the two integers from the user
    a = int(input("Enter the first integer: "))
    b = int(input("Enter the second integer: "))
    #Start the answer with 0
    answer = 0

    print("A", "B")
    print("---")
    print(a, b)
    #run loop until b is not zero
    while b != 0: 
        #loop while 'b' is odd number
        if (b % 2 != 0):
            answer = answer + a 
            print(a*2, b//2) 
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
        #loop while 'b' is even number
        elif (b % 2 == 0):
            print(a*2, b//2)
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
    print("The product is {}.".format(answer))
    repeat = input("Would you like to repeat? (y/n)")
print("Goodbye!")

我正在编写一个使用古埃及方法进行繁殖的程序。 我的程序适用于正数但不适用于负数。 我该如何修复它,以便如果用户输入的两个值都是负数。 我的结果应该给出任意两个正数、负数或一个负数和正数的乘积。 我当前的程序给出了任意两个正值或负a值和正b值的乘积。 但是,当用户输入负b值时,它会产生无限输出。

问题在于下限除法b//2 ,当b为负时,结果将是较低的 integer,因此-0.5将四舍五入为-1 为避免将其转换为int常规除法b = int(b / 2)

删除重复代码后, while循环看起来像这样

while b != 0:
    if b % 2 != 0:
        answer = answer + a
    print(a * 2, int(b / 2))
    a = a * 2
    b = int(b / 2)

编辑

要获得正确的符号,您可以在获取数字并在末尾乘以 1 或 -1 后检查预期的符号。 由于您只检查a的答案,因此您需要努力获得积极a

....
answer = 0
sign = 1 if a * b > 0 else -1
a = abs(a)

while b != 0:
    ....

print("The product is {}.".format(answer * sign))

欢迎来到堆栈溢出!!

这是我在第二个 integer ( b ) 上使用 -2 时程序返回的内容。

A B
---
1 -2
2 -1
4 -1
8 -1
16 -1

如果您注意到,B 保持在 -1,而 A 开始增加 *2 并且它会无限增加,因为 while 循环不会中断。 让我们看看代码

while b != 0: 
        #loop while 'b' is odd number
        if (b % 2 != 0):
            answer = answer + a 
            print(a*2, b//2) 
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
        #loop while 'b' is even number
        elif (b % 2 == 0):
            print(a*2, b//2)
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers

所以在 if 部分,你做 a=a*2 and b=b//2 since b%2.=0 (-1%2 is -1), 但是,为了中断,你需要得到 b =0。 然后你通过做 b=b//2 得到它,正如 Guy 之前所说,问题是当你得到 b=-1(正如你在我的例子中看到的),做 -1//2 会给你 -1 . 而不是执行 1//2 时得到的假设的 0,因为你不会得到 b=0。 该程序永远不会停止增加。

解决方案很简单,就像那个人提到的那样,使用 b=int(b/2)

编辑负数

当你使用乘法时它给你错误符号的原因是这条指令

    while b != 0: 
        #loop while 'b' is odd number
        if (b % 2 != 0):
            answer = answer + a #<--------------- THE INSTRUCTION
            a = a*2 #double every 'a' integers
            # b = b//2 #halve the 'b' integers
            b = int(b/2)
            print(a, b) 
answer = answer + a #<--------------- THE INSTRUCTION

因为你只是通过将 A 相加得到答案,你将有这两种错误的情况----> a 正和 b 负 -> 当它应该是负的时候给你正(A 的符号)----> a negative and b negative -> 当它应该是正数时给你负数(A 的符号)

我试图在谷歌上找到你如何用埃及方法处理负数,但我没有找到任何东西,所以我想你可以用你喜欢的任何一种方法来处理这个问题。 Guy 方法的替代方法是将符号(1 或 -1)乘以取决于 b 的结果,而不是乘法

#------------------Your code-------------------------
    #Start the answer with 0
    answer = 0

    print("A", "B")
    print("---")
    print(a, b)
    #run loop until b is not zero

#------------------The fix proposed-------------------------
    #Start the answer with 0
    answer = 0
    sign = 1 if b > 0 else -1          #---------> the fix
    print("A", "B")
    print("---")
    print(a, b)
    #run loop until b is not zero 

最后,将符号乘以一般答案

#------------------Your code-------------------------
    elif (b % 2 == 0):
            print(a*2, b//2)
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
    print("The product is {}.".format(answer))
    repeat = input("Would you like to repeat? (y/n)")
print("Goodbye!")

#------------------The fix proposed-------------------------
    elif (b % 2 == 0):
            a = a*2 #double every 'a' integers
            b = int(b/2) #---->the previous fix to your first problem
            print(a,b)
            # b = b//2 #halve the 'b' integers
    answer=answer*sign   #---->The another fix: the multiplication i mentioned
    print("The product is {}.".format(answer))
    repeat = input("Would you like to repeat? (y/n)")
print("Goodbye!")

有了这个,你应该让标志正常工作

顺便说一句,我改变你过去只在 if 语句操作结束时打印 (a,b) 的打印指令的原因是为了避免对程序进行冗余操作。

暂无
暂无

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

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