简体   繁体   中英

Can't figure out how to type this sum in python

This sum keeps giving me he wrong answer. We were told to give variables values and workout sums and one of them is A×B÷(C×D) The right answer is 0.0625 and it keeps coming out as 16. I am just wondering why this is happening and what I am doing wrong.

This is what I wrote in python maybe someone can tell me what I am missing.

print(int(A*B/C*D))


A = 2
B = 4
C = 8
D = 16

I am also confused with this one A^((B + C) × D) this is how I wrote it in python (A**(B+C)*D))

Thanks in advance :)

Because * and / have the same precedence. Your formula is interpreted as (((A*B)/C)*D)

Also you should convert your operands to float otherwise you will only get 0 as result.

Firstly, your parentheses are wrong, but that's already been cleared up.

Secondly, I assume you are using Python 2.x, in which integer division returns integers: 1/2 == 0. To fix this, make the numbers floats: A = 2.0; B = 4.0;

Or use Python 3.x.

As some people said above, your formula is wrong, which should be (A B) / (C D) or A B / (C D) withouth int()

Since you have int() to convert the final result, you will never have float number, if the outcome of operation is from 0 to 1(exclusive), int(outcome) is always 0.

In python2.x, at least one of operands should be float to output float result. In python3.x, the python interpreter will automatically convert your result to float if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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