Keep a larger number in a Python division

Asked

Viewed 93 times

-2

@Edit Good evening, I’m having a problem in this code below

It performs two multiplication operations

a * h = X 
b * C = Y

so far so good, but for example, the value of X is less than the value of Y, the wrong account, so I was wondering if it is possible to prioritize the higher number that is the Y to be divided by the lower

valor_de_a = input("Insira o valor de A: ")
valor_de_h = input("Insira o valor de H: ")
valor_de_b = input("Insira o valor de B: ")
valor_de_c = input("Insira o valor de C: ")

tempA = float(valor_de_a)
tempH = float(valor_de_h)
tempB = float(valor_de_b)
tempC = float(valor_de_c)

A_H = tempA * tempH
B_C = tempB * tempC

resultadoA = (A_H / B_C)


print("O resultado de a x h é", A_H)
print("O resultado de b x c é", B_C)

print("O resultado final é", resultadoA)
  • 1

    Cleiton, welcome back, edit your question and put your codes. I recommend you to do the tour

  • I think the min() and max() functions will suit your case

1 answer

2

a * h = X 
b * C = Y

so far so good

No, there is nothing certain "so far" - these expressions may be mathematically valid, but they are a syntax error in Python, and make no sense (although they are syntactically valid), in most programming languages.

In Python, you put an expression - mathematical, or programmatic, more complex, on the side right of the signal =, and one (or more) names for the result of that side expression left from the equals sign. (Just now I will read the rest of your question - it is already quite wrong so far).

Good - the question remains very strange, since you talk about variables "X" and "Y" in your statement, and there are other names in your program - but, I suppose you wanted the end result in resultadoA always be greater than 1, and if A_H is less than B_C, the result is the inverse division.

There are some ways to do this, but perhaps the most readable and concise is with the operator if online, used as a ternary operator.

His Python form is: expressao1 if expressao2 else expressao3 - that is, if the expression 2 has a true value, the result of the expression "if" integer is "expression1" - otherwise it is "expression3" - that is the equivalent in Python to expressao2 ? expressao1 : expressao3 of the languages that inherited the syntax of C (javascript, Java, c#, ...).

That is, in the case of your code snippet above, just rewrite the line that calculates the expression that goes in resultadoA thus:

resultadoA = (A_H / B_C) if A_H >= B_C else (B_C / A_H)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.