1 Replies - 434 Views - Last Post: 11 July 2019 - 07:11 PM Rate Topic: -----

#1 eythoralex   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 11-July 19

python 2 code wont work in python 3

Posted 11 July 2019 - 04:31 PM

I made this code in python 2, to add db values togeather. But when trying to divide by 10 it only shows in whole numbers whitch ruins the calulations. I read somwhere that this was not the case in python 3 so I tryed copying it to python 3 but got an error.
Can you either help me fix the divide issue in python 2 or help me make the code work in python 3? Here is the code...

import math

def divideby10(n):
    return n/10
def powerof(m):
    return 10**m

a = input("db: ")
b = map(divideby10, a)
c = map(powerof, B)/>/>/>
d = sum(c)
e = math.log10(d)
f = e*10


here is the formula I was using:

This post has been edited by jon.kiparsky: 11 July 2019 - 07:06 PM
Reason for edit:: added [code] tags

Is This A Good Question/Topic? 1
  • +

Replies To: python 2 code wont work in python 3

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon

Reputation: 11629
  • View blog
  • Posts: 19,760
  • Joined: 19-March 11

Re: python 2 code wont work in python 3

Posted 11 July 2019 - 07:11 PM

It would be helpful for all of us if you could take the trouble to spell out what "not working" means. What's the problem you're trying to solve?

For example, you might be getting an error message, or you might be getting output that you don't want, or something else might be happening.

In python2, integers are closed under division - this is, to my mind, the correct behavior, but that ship has sailed rather decisively. What this means is that the basic arithmetical operations on integers produce integers, hence 7 divided by 2 = 3, since 3 is the greatest integer i s.t. 2 * i <= 7. It's sensible and rational (so to speak...)

If you want to get a floating-point number back, you have to have a floating point number in the calculation, which you can do by coercing the value you receive from the user to a float:

float(7) / 2 = 3.5

or by specifying that the fixed value you divide by is a float:

7 / 2.0 = 3.5

So that would fix the problem you mentioned with respect to python2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1