Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102081

.format won't display my value with 2 decimal places: Why?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Michael Sullivan <msulli1355@gmail.com>
Newsgroups comp.lang.python
Subject .format won't display my value with 2 decimal places: Why?
Date Thu, 21 Jan 2016 22:57:03 -0600
Lines 46
Message-ID <mailman.204.1453668644.15297.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de +rre1r3PbF9nAFyFqALaCwzgEPX/1GY7QBRYtSLK9oag==
Return-Path <msulli1355@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.061
X-Spam-Evidence '*H*': 0.88; '*S*': 0.00; 'exercise': 0.03; "'''": 0.09; 'here?': 0.09; 'percentage': 0.09; 'subject:Why': 0.09; 'received:209.85.218': 0.10; 'python': 0.10; 'python.': 0.11; 'nearest': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; '>>>': 0.20; 'header:User-Agent:1': 0.26; 'example': 0.26; 'entered': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'skip:( 20': 0.28; 'decimal': 0.29; 'prints': 0.29; 'program,': 0.29; "i'm": 0.30; 'code': 0.30; 'skip:. 10': 0.32; 'run': 0.33; 'message- id:@gmail.com': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'item': 0.35; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'pdf': 0.37; 'doing': 0.38; 'received:209': 0.38; 'wrong': 0.38; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'percent': 0.66; 'results': 0.66; 'price': 0.69; 'discount': 0.81; 'calculations': 0.84; 'examples.': 0.84; 'illustrated': 0.84; 'percentage,': 0.84; 'programs:': 0.84; 'received:192.168.0.3': 0.84; 'subject:value': 0.84; 'thing,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=to:from:subject:message-id:date:user-agent:mime-version :content-type:content-transfer-encoding; bh=ScCJOEj4xr1JAkvk/8xUf5rE7AfEatiT9iBQLeYajhs=; b=nILXzyPvvCuaGks8/jt0gZ3aypJG1NK108EtIvstUJw666ptzlIZs+5nT+QQdWwcQL b9AimUSZyxLJCR24GLS4mVV9wVtQnzo9NYPpAm7i0nOxP3LoK4aPHjiEg/00epRcVbSw ONJrs0TVuVJB5fuhRO8BtJ8JhNnba6cf0ZnH3XQPoV0txLlkDz/VssbfaWRt//dLOhJg ZX2i161tr8Jc9fOHz2DnnWASIu6QnbjhD1tsgMaiWfNGgMeFjRXO0/AhSGCtmTN2OIBd /PMTB95hdS7j5+PunpcxoAh+w28uvGLNtlXVKZc4bywCUI/fwdzg8M+crPAKWZ7YSjPK VvnA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-type:content-transfer-encoding; bh=ScCJOEj4xr1JAkvk/8xUf5rE7AfEatiT9iBQLeYajhs=; b=cDKrtu7gUM9P2p5QwTbGVd+EDEZ4j6gbBpuYOKAT3HvGecGFEHdnbDAwLbd3mgehWP uSZhQ4ZdLHiDWJZmD04Td1u//pnxY2us+XnRi2rpJcrPh+ny6bQnKESMxjovcmkbg3pP rCik5kq/fhR9rtTy3ouRGSMq4Q2gvpDit/yjsN58rGYev06v+Fz/OT+sBS1Cr9JF2mM4 kW2xQ9q6j4ipaI8piJwdj04Jez3HfElJ7wRgM7uQWSo01+dDf2JY4UaHqwBdLmzii0VA ElbLyezD8dAcFpCKEuNIOiP0dqpbpy8WBS+p5TTfJDYJ2jjgCBKjF4winjVFZHbmhfkJ K82w==
X-Gm-Message-State AG10YOR0DdNvuKKNHO0/Dm+uClbxmqw65DXLfUTiO0EvWNNhHeH5fDdZ2F4MkAdkqPBfMA==
X-Received by 10.202.102.228 with SMTP id m97mr739363oik.49.1453438625395; Thu, 21 Jan 2016 20:57:05 -0800 (PST)
User-Agent Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.5.0
X-Mailman-Approved-At Sun, 24 Jan 2016 15:50:43 -0500
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102081

Show key headers only | View raw


Hi.  I'm very very new to python.  I have been working my way through a 
free python pdf file I found (python3handson.pdf) and I'm having trouble 
with one of my programs:


'''discount.py

Exercise 1.14.3.1. * Write a program, discount.py, that prompts the user 
for an original price and
for a discount percentage and prints out the new price to the nearest 
cent. For example if the user enters
2.89 for the price and 20 for the discount percentage, the value would 
be (1- 20/100)*2.89, rounded to two
decimal places, 2.31. For price .65 with a 25 percent discount, the 
value would be (1- 25/100)*.65, rounded
to two decimal places, .49. 10 Write the general calculation code 
following the pattern of the calculations
illustrated in the two concrete examples.

'''

oPrice = 0
newPrice = 0
discount = 0

oPrice = input('What is the original price?  ')
discount = input('How much is this item discounted?  ')

oPrice = float(oPrice)
discount = float(discount)
newPrice = oPrice - (oPrice * discount)

print('The new price is {}' .format(newPrice, '.2f'))


When I run this thing, and I enter 5.00 for original price and .2 for 
the discount, it always results in 4.0.  When I entered my format 
function call directly into the shell, it comes out like I would expect:



 >>> format(4.0, '.2f')
'4.00'


What exactly am I doing wrong here?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

.format won't display my value with 2 decimal places: Why? Michael Sullivan <msulli1355@gmail.com> - 2016-01-21 22:57 -0600

csiph-web