How to make a calculation between a Beautifulsoup value and user input?

Asked

Viewed 17 times

0

I’m doing a program that converts currency values. Then he receives 3 user queries: the selection of the 1st country currency, the selection of the 2nd country currency and the value he wants to convert. But whenever I go to do the conversion calculation or make a test by adding up the user input, either it goes into an except up there or it just adds up the string

import requests
from bs4 import BeautifulSoup
from babel.numbers import format_currency

#Specials variables
choice =  ''
choice1 = ''
en = ''
ent = ''
#Url
url = "https://www.iban.com/currency-codes"
r = requests.get(url)
#r_transfer = requests.get(url_transfer)
#html
html = r.text
#html_transfer = r_transfer.text
#Make a delicious soup
soup = BeautifulSoup(html, 'html.parser')
#soup_transfer = BeautifulSoup(html_transfer, 'html.parser')
tables = soup.find_all('tr')[1:]
data = []

def menu():
  print("")
  try:
    print('')
    print(' Informe pelo número o país de origem da moeda.')
    print('')
    print('')
    ent = int(input("#: "))
  except:
    print('Isso não é um número.')
    menu()
  try:
    if int(ent) <= len(data):
      entint = int(ent)
      global choice
      choice = data[entint]
      print(f'$$$ {ent}')
      print('')
      print(f'(x) ' + choice['city'] )
      print('')
      select_conv()
    else:
      print('Número inválido')
      menu()
  except:
    pass
  return choice


def select_conv():
  print('')
  try:
    print('')
    print('Quer negociar com qual outro país?')
    print('')
    print('')
    en = int((input("# ")))
  except:
    print('Isso não é um número.')
    select_conv()
  try:
    if en <= len(data):
      entint1 = int(en)
      global choice1
      choice1 = data[entint1]
      print('')
      print('')
      value_conv()
    else:
      print('')
      print(' Número inválido')
      select_conv()
  except:
    print('Caiu nesse except')
  return choice1

cotacao = []

def value_conv():
  print(f' Quantos ' + choice["code"] + f' você quer converter para ' + choice1['code'])
  print('')
  print('')
  try:
    ent = int(input('#: '))
  except:
    print('Número inválido')
    value_conv()
  url_transfer = f"https://transferwise.com/gb/currency-converter/{choice['code']}-to-{choice1['code']}-rate?amount={ent}"
  r_transfer = requests.get(url_transfer)
  html_transfer = r_transfer.text
  soup_transfer = BeautifulSoup(html_transfer, 'html.parser')
  try:
    if ent: 
      ent1 = int(ent)
      cot = soup_transfer.find('div', class_="col-lg-6 text-xs-center text-lg-left").find('span', class_="text-success").string
      cotacao.append(cot)
    else:
      print('Apenas valores numéricos')
  except:
    print('oi')

  print(int(cotacao) * ent1)



    
#gera a tabela de países  
for table in tables:
  lst = {
    'city': table.find_all('td')[0].string.capitalize(),
    'code': table.find_all('td')[2].string
  }
  if lst["code"] == None:
    continue
  else:
    data.append(lst)

#enumera por índice
for idx, val in enumerate(data, start = 1):
  print(f"{idx}#", val["city"].title())
menu()

In this case from above, it falls in the except that says print('Fell in that except'). I have tried to put cot = soup_transfer.find('div', class_="col-lg-6 text-xs-center text-lg-left").find('span', class_="text-success").string in a variable and take the int of print(int(cotacao) * ent1), but it interprets as if >quotation was a string. I’m starting in python on several things can be disorganized. So how could I make that calculation?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.