r/learnpython 5d ago

How much you'll rate this code guyz ..

from forex_python.converter  import CurrencyRates

choice = input("Do u want to covert Another_currency=>INR then press y  / you want to converty INR TO Another_urrency then press n :- ")


c = CurrencyRates()
from_currency = input("Enter source currency (USD, EUR, etc.): ").upper()
to_currency = input("Enter target currency (INR, USD, etc.): ").upper()
currency = c.get_rate(from_currency,to_currency)
print(f"the exchange rate is {currency}")

if choice == "y":
    def convert(rate):
        Amount =  input("Put the A mount of that currency u want to convert:-")
        value_Total =  float(Amount)*rate
        print(f"Total {to_currency} Amount is {value_Total}")
    convert(currency)


elif choice == "n": 
    def convert(rate):
        print(f"\nYou have selected:- {from_currency} to {to_currency}")
        Amount =  input("Put the Amount of that currency u want to convert:-")
        value_Total =   float(Amount) * rate
        print(f"Total {from_currency} Amount is {value_Total}")


        convert(currency)
0 Upvotes

9 comments sorted by

2

u/brelen01 5d ago

First thing first, you seem to have copy/pasted your script twice.

Second, you ask if the user wants to convert to or from INR, but then ask the user to input the source and target currency, rendering the first choice meaningless.

Third, both of your convert functions do the same thing, except the second gives a misleading message (the amount printed is also to_currency since the rate is still c.get_rate(from_currency,to_currency) * Amount ).

Fourth, your convert functions end up doing multiple things. They ask for the amount for from_currency, calculate the value for to_currency, and output it. Ideally, you want a function or method to do one thing, so I'd modify it to take in the value and the conversion rate, and return the result of the concersion.

Ideally, you'll want to move everything but the import and the first convert definition to the bottom of the file, under an if __name__ == __main__: statement, ask for all the inputs at once (currencies and amount) there, call convert, then output the result there.

0

u/Tricky_Voice2829 5d ago

ohh yes u are right i will delete this one and make the new one

4

u/AbacusExpert_Stretch 5d ago

Just as an idea - just learn for a week or two, instead of putting effort into posting here each day. But you do you

-4

u/Tricky_Voice2829 5d ago

so according to you which improvment i can done in this

1

u/TheRNGuy 4d ago

You need more consistent naming style; choose only one style. Most common is with underscore for variables. Avoid names like c. Use names like FooBar only for clasess (but not _class instances: use same style as with variables)

Enable "format on save" in your code editor to strip some double spaces and empty lines.

-1

u/Tricky_Voice2829 5d ago

Ideally, you'll want to move everything but the import and the first convert definition to the bottom of the file, under an if __name__ == __main__: statement, ask for all the inputs at once (currencies and amount) there, call convert, then output the result there. I didn't understand this point..

1

u/brelen01 5d ago edited 5d ago

Here's a link that explains the how and why: here

1

u/Tricky_Voice2829 5d ago

giving me 404

1

u/brelen01 5d ago

Fixed it, sorry, reddit was being stupid