Attributeerror: 'Nonetype' Object has no attribute 'saldoConta'

Asked

Viewed 1,019 times

1

I’m starting my POO studies with python and I took a class test ContaBancaria very simple, whose methods would only be withdrawal and deposit. But when I interact with the user, I can’t assign the variables to my attributes. Follow the code below.

Creating the class:

class ContaBancaria:
  def __init__(self, saldoConta: float, valorDesejado: float):
    self.saldoConta = saldoConta
    self.valorDesejado = valorDesejado

  def saque(self):
    self.saldoConta += self.valorDesejado

  def deposito(self):
    self.saldoConta -= self.valorDesejado

  def mostraSaldo(self):
    return self.saldoConta

Interacting with the user:

print("BEM-VINDO AO SISTEMA DE CONTA BANCÁRIA")
print("\n")
print("=" * 20)

valorAtual = float(input("SALDO ATUAL: R$ "))
ContaBancaria(saldoConta=valorAtual, valorDesejado=0)


print("DIGITE\n1. SAQUE\n2. DEPOSITO")
tipo_de_operacao = int(input())
valor_desejado = print('Valor desejado: ')

if tipo_de_operacao == 1:  
  ContaBancaria.saque(valor_desejado)

elif tipo_de_operacao == 2:
  ContaBancaria.deposito(valor_desejado)

else:
  pass

print("OBRIGADO POR UTILIZAR O NOSSO SISTEMA. =) ")

The error presented:

AttributeError: 'NoneType' object has no attribute 'saldoConta'

  • When you charge the class ContaBancaria you do not store the instance in any variable, then call the methods as if they were static methods. Assign the instance to a variable and use it to call the methods.

  • And valor_desejado = print('Valor desejado: ') doesn’t make sense, because valor_desejado will always be None, which is the return of function print. If the idea is to read the value, use input.

  • Ah, that was unintentionally kkk. I fixed it. Thank you!

  • I did that and it worked. I have two more questions: I made each attribute equal to zero, but I didn’t need that, right? And I also tried to print the current balance to the user, but I couldn’t. I printed each method in the class, in if also and didn’t. Any suggestions? !

  • Hi @Beatrizgomes can update the code? So I’ll understand better what you want to do.

  • Hi, William. I’ll send you the updated code, I’ve got it running.

  • First: Do you still have questions? Second, don’t use the answer area like this, edit your question instead. @Beatrizgomes

  • No, no doubt about it. It’s already been solved.

Show 3 more comments
No answers

Browser other questions tagged

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