简体   繁体   中英

new to coding and python. trying to force type (int) for a float answer

balance = 50 - 0.12 * 15
tracks_left = balance / 0.12
round (tracks_left, 0)
print 'you have', tracks_left,"to download"

The answer is you have 401.666666667 to download I've tried round(tracks_left, 0) and int (tracks_left) . What am I doing wrong?

round(tracks_left, 0) does not mutate tracks_left, but instead returns a new value. Likewise with int(tracks_left) .

Try:

tracks_left = round(tracks_left, 0)

Or:

tracks_left = int(tracks_left)

Note: int always rounds down.

round(tracks_left, 0) doesn't change tracks_left . It just returns the value. You have to assign it to something.

>>> balance = 50 - 0.12 * 15
>>> tracks_left = balance / 0.12
>>> tracks_left_rounded = round(tracks_left, 0)
>>> print('You have', tracks_left_rounded)
You have 402.0

You could as well do this:

>>> balance = 50 - 0.12 * 15
>>> tracks_left = round(balance / 0.12)

You can also try int :

>>> tracks_left_rounded = int(tracks_left)
>>> print('You have', tracks_left_rounded)
You have 401

I hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM