Read multiple numbers on the same line with raw_input

Asked

Viewed 9,159 times

6

I need to write a program that reads values and then use them to calculate areas of different geometric figures. My problem is: how to enter the data on the same line? Example: 3.0 4.0 2.0 followed by calculation in the next line

How to write code for Python to read on the same line? It’s really with raw_input?

Like I’m doing:

a = float(raw_input())
b = float(raw_input())
c = float(raw_input()) 
triangulo = (a * c) / 2
print "TRIANGULO:", ("%.3f" % triangulo)
circulo = (3.14159 * c**2 )
print "CIRCULO:", ("%.3f" % circulo)
trapezio = ((a + b) * c) / 2
print "TRAPEZIO:", ("%.3f" % trapezio)
quadrado = b * b
print "QUADRADO:", ("%.3f" % quadrado)
retangulo = a * b
print "RETANGULO:", ("%.3f" % retangulo)

2 answers

6


You can use the raw_input() (in Python 2) or input() (in Python 3) and separate the data through split. I mean, it would look like this:

entrada = raw_input("Digite três números") # lendo os números
# quebrando a entrada em tokens separados por espaço (poderia ser outro separador)
numerosComoString = entrada.split(" ")
# criando uma nova lista com a conversão para float de cada número
numeros = [float(numero) for numero in numerosComoString] 

# atribuindo cada posição da lista a uma variável
a, b, c = numeros
triangulo = (a * c) / 2
print "TRIANGULO:", ("%.3f" % triangulo)
circulo = (3.14159 * c**2 )
print "CIRCULO:", ("%.3f" % circulo)
trapezio = ((a + b) * c) / 2
print "TRAPEZIO:", ("%.3f" % trapezio)
quadrado = b * b
print "QUADRADO:", ("%.3f" % quadrado)
retangulo = a * b

print "RETANGULO:", ("%.3f" % retangulo)

In Python 3, just replace raw_input for input.

Note that no error handling is being done for poorly formatted entries. In actual code, this would be essential.

  • Basically, input values a, b and c must be on the same line.

  • You mean an input line contains 3 numbers separated by space?

  • Yes, I mean that’s how it should be, at least according to the question I should solve in The Huxley (I imagine you know this platform of interaction between students and teachers).

  • I edited the answer. See how it is now. Be sure to have understood each line because these things are fundamental to your next exercises.

  • I did just that before you commented and he accused syntax error in the second line, and saw that in addition to the input I had changed "input" to "values" in the first line, without changing the second. All fixed and running. Thank you very much Plabo! Have a good day.

  • Not for nothing. Don’t forget to vote and mark the answer as accepted if it has helped you. Good studies!

  • It’s done. Thank you.

  • 1

    I’m starting to study Python and your response was very helpful. Grateful!

  • 1

    Very useful, was looking for how to do it, python is gorgeous!!

Show 4 more comments

0

To resolve this issue we can use the following code...

x = list(map(float, input('Digite os valores "A", "B" e "C": ').split()))

triangulo = ((x[0] * x[2]) / 2)
print("TRIANGULO:", "%.3f" % triangulo)
circulo = (3.14159 * x[2]**2)
print("CIRCULO:", "%.3f" % circulo)
trapezio = ((x[0] + x[1]) * x[2]) / 2
print("TRAPEZIO:", "%.3f" % trapezio)
quadrado = x[1] * x[1]
print("QUADRADO:", "%.3f" % quadrado)
retangulo = x[0] * x[1]
print("RETANGULO:", "%.3f" % retangulo)

How to run the algorithm?

When you run this code you get the following message Digite os valores "A", "B" e "C":. At this point you must enter all the três values, in the mesma line, separados for a single space and then press Enter.

From this moment, the entered values will be capturados, converted into float and added to the list x.

From then on, the list values will be recovered through their indices, and then perform the calculations.

Browser other questions tagged

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