-4
I am a beginner in Python and I am developing a calculator, creating a class, within it there are 4 functions, being the 4 operations. And in the main only the basic input. My goal in this program is to use the minimum amount of 'If'. Is there a way to do this or not? But, this is making a mistake that I can’t imagine how it’s happening, It might be in the face but I came here to ask for help:
main.py
from calc import *
numero1 = int(input('Digite um numero: '))
sinal = input('Digite a operação: ')
numero2 = int(input('Digite outro numero: '))
retornaValor = 0
retornaValor = Calculadora
print(retornaValor)
calc.py
class Calculadora:
def __init__(self,numero1,numero2):
self.a = numero1
self.b = numero2
def soma(self,numero1,numero2):
soma = self.a + self.b
print('Resultado: ',soma)
def subtrai(self,numero1,numero2):
subtrai = self.a - self.b
print('Resultado: ',subtrai)
def divisao(self,numero1,numero2):
divisao = self.a / self.b
print('Resultado: ',divisao)
def multiplica(self,numero1,numero2):
multiplica = self.a * self.b
print('Resultado: ',multiplica)
Console:
Digite um numero: 100
Digite a operação: +
Digite outro numero: 30
''<class 'calc.Calculadora'>'' (????)
In that code there are no more
if
, what exactly you want to reduce?– hkotsubo
Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.
– Bacco
I was actually trying to create a calculator without the intense use of
if
to not be so messy, or was even doing without the use ofif
, but I don’t think it’s possible...– Luiz15K
It has several ways of doing, an example (half bad, but "almost without if"): https://ideone.com/HPUKYV - I don’t really think I needed a class, but to do what. In fact, the 2 answers below are not good (although you think they are, because you even accepted one), because it does not make sense to create the calculator passing the values and then call a method calling those same values (being that they are already part of the class itself) - this, in fact, is one of the problems of using a class... One of them still says it is "obliged" to put default values (
n1=1, n2=2
), what is not true...– hkotsubo
Making
Calculadora(n1, n2)
you have a class that only does math with those 2 numbers. If the idea is to calculate with any numbers, it should only beCalculadora()
and she gets the numbers in the methods (as I did in the example I put above). The answers mix both solutions and in the end only unravel (why doCalculadora(n1, n2)
if later you will callsoma(n1, n2)
(ie, saved the numbers in the constructor, then ignore them in the methods)? Even usingn1=1, n2=1
, you’re still holding values that will be ignored later...– hkotsubo
I just saw that example of yours, I found it very strange first kkkk, in case then the way in this program and at least use each if for operation then? I appreciate the expensive help mainly explaining about the use of classes in a better way to understand yourself
– Luiz15K
A
if
for each operation would be the simplest, something likeif operacao == '+': faz a soma, etc
. Theif
is one of the most basic things in programming, there is no reason to want to avoid at all costs. If it makes sense, use– hkotsubo
I got it, man, thanks for your help... My real doubt was whether there was any way to reduce the If but as you said above, it will usually always make sense by the way kkk... Abs
– Luiz15K