-4
I am working on a calculator that needs to receive an operation (such as SUM, DIV, LOG10 etc.) and operators on the same line, eg: SUM 2 4
But when I get LOG10 or ROOT the user will only type an operator, example: ROOT 36
The code works in the calculating part and when the user type 3 strings in the input, when he type two (as in the case of the root) the program generates error. My difficulty is to have my input delete this extra variable if the user does not type it.
The code is:
operation, n1_str, n2_str = input().split(" ")
n1 = float(n1_str)
n2 = float(n2_str)
while((operation != "RAZ") | (operation != "LOG10")):
if(operation == "SUM"):
SUM = n1 + n2
print("%.2f" %(SUM))
elif(operation == "DIF"):
DIF = n1 - n2
print("%.2f" %(DIF))
elif(operation == "DIV"):
DIV = n1 / n2
print(DIV)
elif(operation == "MULT"):
MULT = n1 * n2
print("%.2f" %(MULT))
elif(operation == "POT"):
POT = n1 ** n2
print("%.2f" %(POT))
break
while((operation == "RAIZ") | (operation == "LOG10")):
n1 = int(input())
if (operation == "RAIZ"):
RAIZ = n1 ** 0.5
print("%.2f" %(RAIZ))
else:
LOG10 = math.log(n1, 10)
print("%.2f" %(LOG10))
break
It’s so much easier to ask for the data in isolation. I don’t know why this craze of people who use Python started wanting to take one data and manipulate it. Recently nobody did this and was happier. So much can go wrong doing so. So either do it another way or accept the mistake and understand that the person should be responsible for typing right, that for an exercise all right, but for something real has to do all treatment, not just partial.
– Maniero
Yes, it would be much easier to take alone. But the exercise asks for entry in this way. :/
– Isabela Pereira dos Santos
interesting, I didn’t even know that python accepted the operator "or" as
|
, always usedor
– yoyo
Isabella, as it was not mentioned in any of the answers below, I think it important to mention that your use of the
while
is completely and absolutely unnecessary. Note that no matter what theinput()
, it will always rotate once. A loop that only rotates once is not a loop. A simpleif
there would solve– yoyo
@Yoyo In fact the
|
is a bitwise operator and only "works" by coincidence, because the boolean in Python is a subclass ofint
. Remembering that the logical operators like theor
andand
are not exactly the same, as they have the characteristic of being Circuit short (only evaluates the second condition if necessary), and in Python they return the value of the evaluated expression (different from bitwise, which always returns numbers)– hkotsubo
@hkotsubo I found it interesting, I’ll take the cue and link that post, which I found interesting, although not in python, and that. Very instructive! You are exposed to whom you are interested
– yoyo