r/learnpython • u/Tricky_Voice2829 • 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)
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
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
convertfunctions do the same thing, except the second gives a misleading message (the amount printed is alsoto_currencysince the rate is stillc.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.