Guest User

Untitled

a guest
Apr 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. parser = argparse.ArgumentParser(description='Currency convertor. Before using the program read the instruction -h')
  2. parser.add_argument('--amount', type=float, help='The amount you want to convert.')
  3. parser.add_argument('--input_currency', help='Currency to conver. The format is three letters example. USD')
  4. parser.add_argument('--output_currency', type=str, help='The target currency')
  5.  
  6. args = parser.parse_args()
  7. currency.append('EUR')
  8. rates.append(1)
  9. if args.input_currency == None:
  10. print('Error: Wrong format or currency not suported. The format is three letters. example: USD')
  11. print('Supported currencies:', currency)
  12. quit()
  13. elif len(args.input_currency) != 3:
  14. print('Error: Wrong format. The format is three letters. example:', currency)
  15. quit()
  16. else:
  17. d = dict(zip(currency, rates))
  18. rate_i = float(d.get(args.input_currency.upper()))
  19.  
  20. if args.output_currency == None:
  21. del d[(args.input_currency.upper())]
  22. output= {key:((float(value) / rate_i) * args.amount) for key, value in d.items()}
  23. else:
  24. if len(args.output_currency) != 3:
  25. print('Error: Wrong format. The format is three letters. example:', currency)
  26. quit()
  27. else:
  28. rate_o = float(d.get(args.output_currency.upper()))
  29. output= {args.output_currency: (rate_o / rate_i) * args.amount}
  30. result={
  31. "input": {
  32. "amount": args.amount,
  33. "currency": args.input_currency
  34. },
  35.  
  36. "output": output
  37. }
  38. print(json.dumps(result, indent=4))
Add Comment
Please, Sign In to add comment