r/pythontips • • Jun 01 '20

Python3_Specific Created my first real program python program! A currency converter that checks the internet for live exchange rates. Any tips ?

from urllib.request import urlopen as uReq

from bs4 import BeautifulSoup as soup

playing = True

#Establishing a connection

my_url = 'https://ron.fxexchangerate.com'

uClient = uReq(my_url)

page_html = uClient.read()

uClient.close()

page_soup = soup(page_html, "html.parser")

# Grabbing the first set of ratings from the Currency Pair Side Table

container = page_soup.find('div', class_='sider')

numbers = container.findAll('span')

USD_EURalpha = numbers[1].text

GBP_USDalpha = numbers[2].text

USD_EUR = float(USD_EURalpha) # ~0.897

GBP_USD = float(GBP_USDalpha) # ~1.240

EUR_GBPalpha = numbers[7].text

EUR_GBP = float(EUR_GBPalpha) # ~0.897

#Grabbing the second set of ratings from the Main Table

container_two = page_soup.find('tbody',class_='project')

numbers_ron = container_two.text

RON_USD = float(numbers_ron[27:33]) # ~ 0.229

RON_EUR = float(numbers_ron[63:70]) # ~ 0.205

RON_GBP = float(numbers_ron[108:114]) # ~ 0.184

#Inverting the rates

EUR_USD = 1 / USD_EUR

USD_GBP = 1 / GBP_USD

USD_RON = 1 / RON_USD

EUR_RON = 1 / RON_EUR

GBP_RON = 1 / RON_GBP

GBP_EUR = 1 / EUR_GBP

#Setting the loop

def program():

while playing == True :

	first_input = input(" Welcome, please select the currency you want to convert from: \\n press 'd' for dollar  \\n press 'l' for lei  \\n press 'e' for euro  \\n press 'p' for pounds  ")

	second_input = input("Following the same patern as before, please select the currency you want to convert to:  ")

	suma = float(input("Please type the ammount in numbers  "))   

		

	def result():

		result = \[\]

		if first_input.lower()\[0\] == 'e' and second_input.lower()\[0\] == 'l':

result = float(suma) * EUR_RON

		elif first_input.lower()\[0\] == 'd' and second_input.lower()\[0\] == 'l':

result = float(suma) * USD_RON

		elif first_input.lower()\[0\] == 'l' and second_input.lower()\[0\] == 'e':

result = float(suma) * RON_EUR

		elif first_input.lower()\[0\] == 'l' and second_input.lower()\[0\] == 'd':

result = float(suma) * RON_USD

		elif first_input.lower()\[0\] == 'e' and second_input.lower()\[0\] == 'd':

result = float(suma) * EUR_USD

		elif first_input.lower()\[0\] == 'd' and second_input.lower()\[0\] == 'e':

result = float(suma) * USD_EUR

		elif first_input.lower()\[0\] == 'd' and second_input.lower()\[0\] == 'p':

result = float(suma) * USD_GBP

		elif first_input.lower()\[0\] == 'p' and second_input.lower()\[0\] == 'd':

result = float(suma) * GBP_USD

		elif first_input.lower()\[0\] == 'e' and second_input.lower()\[0\] == 'p':

result = float(suma) * EUR_GBP

		elif first_input.lower()\[0\] == 'p' and second_input.lower()\[0\] == 'e':

result = float(suma) * GBP_EUR

		elif first_input.lower()\[0\] == 'l' and second_input.lower()\[0\] == 'p':

result = float(suma) * RON_GBP

		elif first_input.lower()\[0\] == 'p' and second_input.lower()\[0\] == 'l':

result = float(suma) * GBP_RON

		else:

print ("Please choose between 'l','d','p' or 'e' ")

		print (f'result = {result}')

		

	result()

		

	continuare = input("Go again? 'y' or 'n'")

		

	if continuare.lower()\[0\] == 'n':

		playing == False

		break

	else:

		playing == True

program()

43 Upvotes

8 comments sorted by

5

u/Anonymous12c19 Jun 01 '20

This is pretty cool! Do you use GitHub tho?

1

u/uta44 Jun 01 '20

not really, should I ? :))

5

u/f4tj3suz Jun 01 '20

yea, check it out it's a good platform for sharing code ;-)

3

u/the29devil Jun 01 '20

That's so cool!!! How do I learn this kind of stuff... I have just finished my basic course on python. Like I can write small programs of palindrome number or fibonacci series. What's the next step? Any tips 😬

5

u/uta44 Jun 01 '20

well then I think that we might be on the same level haha because I'm just finishing my first Python course (on Udemy) aswell. What I did was firstly create the program that can just convert my input currency into another currency without getting the data from the internet , using exchange rates that I gave manually. After that I literally wrote on Google "python how to grab data from a site" haha and I started to learn about what data scrapping is, what beautifulsoup is etc. Just type on YouTube "Python BeautifulSoup tutorial" and the first one is the one that I mostly used for this program. After a lot of trial and error I finally got it to work and I m soooo happy haha

1

u/the29devil Jun 01 '20

Good for you bruh... I will try my best to learn this stuff. My college gives shit assignments that were obsolete years ago, therefore don't get much time to fidel around something actually productive.

2

u/tanvirphysics Jun 01 '20

Very well done!

1

u/uta44 Jun 01 '20

Thank you!