Multiplication and division result with two boxes after the comma

Asked

Viewed 196 times

2

Hello everybody all right?

I’m doing an activity for my python course, it’s a script that basically calculates the area of a wall in meters and tells how many liters of paint will be used. The problem is that when I type a very high value of height and width, type, 543.404 x 456.435 the script returns me the following result of area 2.3e+05, I wanted all the results to come with floating points and only two houses after the comma, follows my code. Obs: in my program 1 liter of paint paints 2m2 of area, so the variable t = 2.

Note: Someone helps me post the code correctly here on the site.

print('Bem vindo ao programa de orçamento\nde tintas: ')

l = float(input('Digite a largura da parede em metros: '))

print('Muito bem!')

a = float(input('Agora digite a altura da parede: '))

m = (l * a)

print ('Certo! Você tem uma área de: {:.2} metros' .format (l * a))

t = 2

print ('Para pintar esta parede, você vai\nprecisar de: {:.2} litros de tinta!'.format(m / t))

2 answers

3

This is because by default, when printing floats like this, Python tries to use scientific notation to shorten the result.

You can force it to give you the full result using {:.2f} instead of just {:.2}:

print('Certo! Você tem uma área de: {:.2f} metros'.format (l * a))
t = 2
print('Para pintar esta parede, você vai\nprecisar de: {:.2f} litros de tinta!'.format(m / t))

2

You can use the round function to round the values, then display the values correctly with the print function. The characters that appear are the values in scientific notation.

print('Bem vindo ao programa de orçamento\nde tintas: ')

l = float(input('Digite a largura da parede em metros: '))

print('Muito bem!')

a = float(input('Agora digite a altura da parede: '))

m = round((l * a),2)

print ('Certo! Você tem uma área de: ',m,'metros')

t = 2
litros =round( (m/t) , 2)

print ('Para pintar esta parede, você vai\nprecisar de: ',litros,' litros de tinta!')
  • and to post the code here correctly, as I do?

  • Select your code and press the top tab on the keys ( {} )

  • @Leandrosargi or install my extension (Firefox or Chrome), selects the text and presses Tab :)

  • I don’t understand this question of keys, is that I use the phone to post things here, and the code always comes out as text, then someone always revises, I want to post directly.

  • Yeah, I don’t know how it looks on the phone. I never used it! Choose some correct answer!

Browser other questions tagged

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