How to turn a String list into a Float list?

Asked

Viewed 941 times

0

If I run this code, it will give a string list:

inserir a descrição da imagem aqui
So that I can not make the average number correctly (since it is in string). What can I do to have the number printed in float/int?

Does this "bug" happen only with Python.3x? What would happen if I migrated to 2x.? It would print in float?

   vet_aluno = [0]*2
for i in range(2):
    vet_aluno[i] = input ("Digite o nome do(a) aluno(a): ")

vet_nota1 = [0]*2
for k in range(2):
    vet_nota1[k] = input("Digite a nota de " + str(vet_aluno[0]) + ": ")


vet_nota2 = [0]*2
for a in range(2):
    vet_nota2[a] = input("Digite a nota de " + str(vet_aluno[1]) + ": ")

#Calculando a média
media1 = media(vet_nota1[0],vet_nota1[1])
media2 = media(vet_nota2[0],vet_nota2[1])

2 answers

1


In Python3, when you use the input function to ask for a user input, it always returns a string.
If you want you can turn the string into a float, as follows:

vet_nota1 = [0]*2
for k in range(2):
    vet_nota1[k] = float( input("Digite a nota de " + str(vet_aluno[0]) + ": "))

vet_nota2 = [0]*2
for a in range(2):
    vet_nota2[a] = float( input("Digite a nota de " + str(vet_aluno[1]) + ": "))

However, doing just that, you’ll be confident that the user will always type a string that can be transformed into a float.
If the user type anything else, like 'Test', when asked for the note, there will be an exception of the Valueerror type and your program will be finished.


In Python2, there are 2 functions to ask for user input: input and raw_input.

raw_input works like Python3 input, just returns the string that was typed.
The input tries to interpret what was typed by the user as a Python expression. So, if he typed something in the shape of a float, it would already return the input as a float, not as a string.

In Python2, you could have written the following program:

vet_aluno = [0]*2
for i in range(2):
    vet_aluno[i] = raw_input("Digite o nome do(a) aluno(a) {}: ".format(i))

vet_nota1 = [0]*2
for k in range(2):
    vet_nota1[k] = input("Digite a nota de " + str(vet_aluno[0]) + ": ")

vet_nota2 = [0]*2
for a in range(2):
    vet_nota2[a] = input("Digite a nota de " + str(vet_aluno[1]) + ": ")
  • In that case it would be more convenient and practical to use Python2?

  • 1

    The two are equivalent at this point. In Python3 you can also easily interpret the input as float, as shown, and if the user does not enter a float in Python2, you will also have problems if you do not check that what was actually typed is the float, then that shouldn’t be a criterion for deciding between one and the other. The best would be to use Python3 whenever possible, since it is the latest version. More and more people will stop supporting Python2 in the future.

0

import requests
from bs4 import BeautifulSoup
page = requests.get('http://dolarhoje.com')
soup = BeautifulSoup(page.content, 'html.parser')
dollar = float(soup.find_all('p')[2].get_text().split()[9][:-1].replace(',', '.'))

Browser other questions tagged

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