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.– Woss
And
valor_desejado = print('Valor desejado: ')
doesn’t make sense, becausevalor_desejado
will always beNone
, which is the return of functionprint
. If the idea is to read the value, useinput
.– Woss
Ah, that was unintentionally kkk. I fixed it. Thank you!
– BeatrizGomes
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? !– BeatrizGomes
Hi @Beatrizgomes can update the code? So I’ll understand better what you want to do.
– William Teixeira
Hi, William. I’ll send you the updated code, I’ve got it running.
– BeatrizGomes
First: Do you still have questions? Second, don’t use the answer area like this, edit your question instead. @Beatrizgomes
– William Teixeira
No, no doubt about it. It’s already been solved.
– BeatrizGomes