I can not solve this error in Python, I am a beginner

-1

Well I need to do this multiplication but give the following error:

Traceback (most recent call last):
  File "C:\Users\Guilherme\Desktop\dasdsa.py", line 17, in <module>
    conta = (trans1) * trans2
TypeError: can't multiply sequence by non-int of type 'tuple'

What do I do? Do I need a type of variable that does the comma multiplication? because I tried to float but it still did not work, I could not convert trans1 to float, what are my options?

MY CODE:

import urllib.request

page = urllib.request.urlopen("http://dolarhoje.com/")
text = page.read().decode("utf8")

price = text[9938:9942]

trans1 = (price)
tuple(price)

usertap = input("Coloque o real aqui:")
trans2 = tuple(usertap)
conta = (trans1) * trans2

print(conta)
    
asked by anonymous 19.10.2015 / 22:38

3 answers

1

If the error is due to the comma in place of the point, just replace it. For the code that you show, there is no need to use tuples, which could look like this:

import urllib.request

page = urllib.request.urlopen("http://dolarhoje.com/")
text = page.read().decode("utf8")

raw_price = text[9938:9942]
# substitui a pontuação e converte para float
price = float(raw_price.replace(',', '.'))

raw_usertap = input("Coloque o real aqui: ")
# o mesmo procedimento feito anteriormente só que com a entrada do usuário
usertap = float(raw_usertap.replace(',', '.'))

conta = price * usertap

print(conta)
    
20.10.2015 / 00:39
0

In your program, trans1 and trans2 are tuples (apparently tuples of characters). To make the counter you need to be numbers

trans1 = float(price)
trans2 = float(usertap)
    
19.10.2015 / 22:51
0

Correct would be to turn both variables into the same type (in this case, or an integer or floating point). Something like this:

trans1 = Float(price)
trans2 = Float(usertap)

However, the portion of the page you chose (Storing at price) is resulting in letters, not numbers. Probably due to changes in the site or something ... I would recommend using BeautifulSoup at the time of handling the HTML file to avoid this kind of error.

    
24.10.2017 / 02:11