Problems in PYTHON end result

Asked

Viewed 76 times

-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'>'' (????)
  • 1

    In that code there are no more if, what exactly you want to reduce?

  • 1

    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.

  • 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 of if, but I don’t think it’s possible...

  • 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...

  • 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 be Calculadora() 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 do Calculadora(n1, n2) if later you will call soma(n1, n2) (ie, saved the numbers in the constructor, then ignore them in the methods)? Even using n1=1, n2=1, you’re still holding values that will be ignored later...

  • 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

  • A if for each operation would be the simplest, something like if operacao == '+': faz a soma, etc. The if 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

  • 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

Show 3 more comments

1 answer

0


I just tested the code part quickly, apparently you have an indentation problem there. In case you don’t know, trying to explain in a very playful way what indentation is, it is a kind of hierarchy, in which external functions lie to the left of an inner function.

The inner functions are direct consequences or only exist from your outer function, you also need to create a module to define the values of n1 and N2 in the new object.

class Calc(object):

# Instanciando a classe Calc
 def __init__(self,n1=1,n2=1): # Quando se trata de valores, temos que colocar um valor padrão apenas pela sintaxe do Python.
# Geralmente esse valor será 1.    
   self.n1 = n1
   self.n2 = n2
      
# Criando um comando para definir os números dos objetos da calsse Calc    
# Logo abaixo pedindo para o que uma soma seja definida e que ela seja mostrada quando o esse comando for executado.
 def setn(self,n1,n2):
    self.n1 = n1
    self.n2 = n2
    print(n1+n2)
    
calculator = Calc() # Criando um objeto calculator da classe Calc()

calculator.setn(n1=float(input()),n2=float(input())) # Pedindo para o objeto calculator executar o módulo setn e definindo os valores dos números.

This is an example of what you should do with all the other items to make this part of the code functional.

  • Forgive me the formatting here in the stack but the identation that I put this correct, sorry for vdd, gave me a new idea. I really appreciate the help

  • I am a mathematician, not a programmer, so I would probably not create a subtraction or division function, since they are the inverse of addition and multiplication. Anyway I hope I helped something. : 3

  • Gratitude for helping :)

Browser other questions tagged

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