[英]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!")
I am writing a program that uses Ancient Egyptian method to multiply.我正在编写一个使用古埃及方法进行繁殖的程序。 My program works for positive numbers but not negative.
我的程序适用于正数但不适用于负数。 How do I fix it so that if both inputted values of user are negative.
我该如何修复它,以便如果用户输入的两个值都是负数。 My result should give the product of any two positive, negative or one negative and positive number.
我的结果应该给出任意两个正数、负数或一个负数和正数的乘积。 My current program gives the product for any two positive values, or negative
a
value and positive b
value.我当前的程序给出了任意两个正值或负
a
值和正b
值的乘积。 However, when user enters a negative b
value, it produces infinite outputs.但是,当用户输入负
b
值时,它会产生无限输出。
The issue is with the floor division b//2
, when b
is negative the result will be the lower integer, so -0.5
will be rounded to -1
.问题在于下限除法
b//2
,当b
为负时,结果将是较低的 integer,因此-0.5
将四舍五入为-1
。 To avoid it cast to an int
a regular division b = int(b / 2)
.为避免将其转换为
int
常规除法b = int(b / 2)
。
After removing duplicate code the while
loop looks like that删除重复代码后,
while
循环看起来像这样
while b != 0:
if b % 2 != 0:
answer = answer + a
print(a * 2, int(b / 2))
a = a * 2
b = int(b / 2)
Edit编辑
To get the correct sign you can check the expected sign after getting the numbers and multiple by 1 or -1 at the end.要获得正确的符号,您可以在获取数字并在末尾乘以 1 或 -1 后检查预期的符号。 Since you only check
a
for the answer you need to work on a positive a
由于您只检查
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))
Welcome to Stack Overflow!!欢迎来到堆栈溢出!!
This is what the program returned when I used -2 on the second integer ( b ).这是我在第二个 integer ( b ) 上使用 -2 时程序返回的内容。
A B
---
1 -2
2 -1
4 -1
8 -1
16 -1
If you notice, B stays on -1, while A starts increasing *2 and it does indifinitely because the while loop doesn't break.如果您注意到,B 保持在 -1,而 A 开始增加 *2 并且它会无限增加,因为 while 循环不会中断。 Let's look at the code
让我们看看代码
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
So on the if section, you do a=a*2 and b=b//2 since b%2.=0 (-1%2 is -1), However, for the while to break, you need to get b=0.所以在 if 部分,你做 a=a*2 and b=b//2 since b%2.=0 (-1%2 is -1), 但是,为了中断,你需要得到 b =0。 and you get that by doing b=b//2, The problem, as Guy said before, is that when you get b=-1 (as you saw on my example), doing -1//2 will give you -1.
然后你通过做 b=b//2 得到它,正如 Guy 之前所说,问题是当你得到 b=-1(正如你在我的例子中看到的),做 -1//2 会给你 -1 . instead of the supposed 0 that you'll get if you do 1//2, Since you won't get b=0.
而不是执行 1//2 时得到的假设的 0,因为你不会得到 b=0。 the program never stops multiplying.
该程序永远不会停止增加。
The solution is simple, as guy mentioned, use b=int(b/2)解决方案很简单,就像那个人提到的那样,使用 b=int(b/2)
EDIT for the negative numbers编辑负数
The reason it gives you the wrong sign when you are using multiplication is this instruction当你使用乘法时它给你错误符号的原因是这条指令
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
since you get your answer by just adding up A, you will have these two wrong scenarios ----> a positive and b negative ->gives you positive (the sign of A) when it should be negative ----> a negative and b negative -> gives you negative (the sign of A) when it should be positive因为你只是通过将 A 相加得到答案,你将有这两种错误的情况----> a 正和 b 负 -> 当它应该是负的时候给你正(A 的符号)----> a negative and b negative -> 当它应该是正数时给你负数(A 的符号)
I tried to find on google how you handled negative numbers with the egyptian method but i didn't find anything, so I guess you can handle that issue with whichever method you prefer.我试图在谷歌上找到你如何用埃及方法处理负数,但我没有找到任何东西,所以我想你可以用你喜欢的任何一种方法来处理这个问题。 An alternative to Guy method is multiplying the sign (1 or -1) to the result depending of b, not the multiplication
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
and at the end, you multiply the sign with the general answer最后,将符号乘以一般答案
#------------------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!")
With that, you should have the signs working properly有了这个,你应该让标志正常工作
btw, the reason I changed the print insruction that you used to just print (a,b) at the end of the operation of the if sentence is to avoid redundant operations on the program.顺便说一句,我改变你过去只在 if 语句操作结束时打印 (a,b) 的打印指令的原因是为了避免对程序进行冗余操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.