F
F
From Prog2021-05-15 23:42:34
Python
From Prog, 2021-05-15 23:42:34

What is the best way to end the program?

What is the best way to do the 3 currency conversion method?
Is it just to prescribe everything through if choise == usd:or is there some other way, please tell me

class Converting:
  def data(self):
    choise = (input('Введите валюту, которую хотите купить(rub, usd, eur): '))
    choise_lower = choise.lower()
    if choise_lower != 'rub' and choise_lower != 'usd' and choise_lower != 'eur':
      exit('Такой валюты у нас нет!')
    currency = (input('Какая у вас валюта(rub, usd, eur): '))
    currency_lower = currency.lower()
    if currenc_lowery != 'rub' and currency_lower != 'usd' and currency_lower != 'eur':
      exit('Такую валюту мы не принимаем!')
    amount = int(input('Сколько вы хотите купить {0}: '.format(choise)))

  def answer(self):
    rub = 1
    eur = 90
    usd = 75

    
    print('Стоимость покупки {} {} будет составлять {}'.format(self.amount, self.choise_lower, ))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-05-16
@FromProg

You have OOP here for the sake of OOP.
In general, it is not clear why the Converting class is needed, but you can still write it in a few lines. Of course, maybe this is part of some big project where it is justified, but I somehow strongly doubt it.
To convert any values, you need to get involved in one thing. To some abstract value.
Create a dictionary where you will store the value of all your currencies relative to the "reference" value. Here we have shown that the ruble contains exactly one such value, the euro - 90, the dollar - 75 Now if the user entered, for example, 'usd', just refer to the dictionary by this key and get how many of these values ​​are in one dollar. Then comes the banal mathematics, which you yourself can implement.
values ={'rub': 1, 'eur': 90, 'usd': 75}
The check for correct input can now be shifted to the key in dict construction of this dictionary.
You can add some more values ​​​​to this dictionary and you don’t have to write additional conditions to check the correctness of the input. If you have python version 3.6 or later, use f-strings
instead of the format() method .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question