SillyNoob
SillyNoob

Reputation: 13

Integer from tuple, divided, yields integer instead of float?

In the code below,

abc = (1,2,3)
a = abc[0]
b = abc[1]
c = abc[2]

print('%d, %d, %d' %(a,b,c)) # gives 1,2,3 as expected

a /= 3
b /= 3
c /= 3

print('%d, %d, %d' %(a,b,c)) # gives 0,0,1... which is NOT expected

I'm trying, however, to have a, b, c be 0.333, 0.6666, 1 respectively.

This problem persists even when I:

I'm even using python 3, so an int/int division should return a float regardless.

Upvotes: 1

Views: 197

Answers (3)

Arihant
Arihant

Reputation: 11

Use %f inside print statement

print('%f, %f, %f', %(a,b,c))

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You are using %d so the output is printed as ints/decimal just use print:

print(a,b,c)

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

Your print statements say to display the values as integers (%d); try using %f instead.

Upvotes: 9

Related Questions