I can’t fix this bug in Python, I’m a beginner

Asked

Viewed 320 times

-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 multiplication with comma? because I tried to put in float but it still didn’t work, I couldn’t convert the trans1 to float, what 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)
  • BTW, has a line with a tuple(price) alone that does nothing. The title of your question could also be more specific.

3 answers

1

If the error is based on the comma instead of the point, just replace it. By the code you show, there is no need to use tuples:

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)

0

The correct one would be to transform both variables to the same type (in this case, or an integer or floating point). Something like this:

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

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

0

In your program, trans1 and trans2 are of the tuple type (apparently, tuples of characters). To do against you need to be numbers

trans1 = float(price)
trans2 = float(usertap)
  • So, I already tried to float it but give the following error: Traceback (Most recent call last): File "C: Users Guilherme Desktop dasdsa.py", line 11, in <module> float(price) Valueerror: could not Convert string to float: '3,88'

  • The problem is that it cannot convert the comma. If it were a point it would work.

  • So the only way would it be, would I change what’s inside price? In this case this 3.87. Would I have to change it? Only then would it work?

  • 1

    That’s one way to solve the problem.

Browser other questions tagged

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