M
M
mikki200412021-03-16 15:41:36
Python
mikki20041, 2021-03-16 15:41:36

AttributeError: 'str' object has no attribute how to fix?

Recently started learning OOP in Python. I'm trying to write a program in which you need to calculate how much the user needs to pay for the purchase of one of the two currencies. But the error AttributeError: 'str' object has no attribute 'exchange_usd' is displayed, please tell me what is wrong in the code?

class Converter():
    
    def __init__(self, eur, usd):
        self.usd = usd
        self.eur = eur
    
    def exchange_usd(self, usd):
        self.usd*70
        print(f"Стоимость покупки {self.usd}$ будет составлять {purchase}")
    
    def exchange_eur(self, eur):
        self.eur*80
        print(f"Стоимость покупки {self.eur}€ будет составлять {purchase}")

purchase = Converter("eur", "usd")
bank_user = input('Какую валюту хотите купить: ')

if bank_user == 'usd':
    purchase = input('Сколько хотите купить: ')
    purchase.exchange_usd()

elif bank_user == 'eur':
    purchase = input('Сколько хотите купить: ')
    purchase.exchange_eur()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-03-16
@mikki20041

Firstly, there is a line in purchase, after input is made
In the class, the purchase name is not defined, what the editor says
self.usd*70does not change anything,
purchase.exchange_usd()no arguments were passed,
self.usd*70multiplies the string "usd" 70 times

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question