3

I am trying to write a method in Python 3.2 that encrypts a phrase and then decrypts it. The problem is that the numbers are so big that when Python does math with them it immediately converts it into scientific notation. Since my code requires all the numbers to function scientific notation, this is not useful.

What I have is:

coded = ((eval(input(':'))+1213633288469888484)/2)+1042

Basically, I just get a number from the user and do some math to it.

I have tried format() and a couple other things but I can't get them to work.

EDIT: I use only even integers.

Dan G.
  • 982
  • 1
  • 8
  • 20
  • One minor comment: Python never displays integers in scientific notation. Your problem here is that you don't have an int, you have a float. So, the question isn't entirely accurate. But it's pretty clear what you're asking, and mgilson's answer explains everything. – abarnert Sep 28 '12 at 01:07

2 Answers2

8

In python3, '/' does real division (e.g. floating point). To get integer division, you need to use //. In other words 100/2 yields 50.0 (float) whereas 100//2 yields 50 (integer)

Your code probably needs to be changed as:

coded = ((eval(input(':'))+1213633288469888484)//2)+1042

As a cautionary tale however, you may want to consider using int instead of eval:

coded = ((int(input(':'))+1213633288469888484)//2)+1042
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • There are at least three different blogs out there called "Eval is Evil" or "Why Eval is Evil", all of which are probably worth reading. But briefly: it makes things harder to debug, it opens your code to attack by malicious users, and it's less efficient. – abarnert Sep 28 '12 at 00:13
  • 4
    consider `eval("os.system('rm -rf *')")` – wim Sep 28 '12 at 00:15
  • 3
    @DanG -- To add to wim's response, `eval("__import__('os').system('rm -rf *')`. (in other words, you don't even need to have `os` imported to be vulnerable. – mgilson Sep 28 '12 at 00:23
  • mgilson and @wim im kinda new to this stiff so you really just confusing me – Dan G. Sep 28 '12 at 00:36
  • 2
    @DanG. -- Basically, if a user typed that command into your program, it would delete everything in the directory where your program is running. – mgilson Sep 28 '12 at 00:40
  • Wow. that could be a disaster! – Dan G. Sep 28 '12 at 01:21
0

If you know that the floating point value is really an integer, or you don't care about dropping the fractional part, you can just convert it to an int before you print it.

>>> print 1.2e16
1.2e+16
>>> print int(1.2e16)
12000000000000000
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    Really? So `print int(1.2e24)` will display `1200000000000000000000000`? No. The answer is to keep from converting the int to float in the first place; once you've done that, you've permanently lost information, and there is no way to get it back. – abarnert Sep 28 '12 at 01:09
  • 1
    @abarnert, yes naturally that's the best answer. I was providing an alternate answer for the question asked in the title rather than the specific circumstances posed by the OP. – Mark Ransom Sep 28 '12 at 01:12
  • @MarkRansom yes that would allow me to print a float but as abarnert said there is too much lost data for that to be useful – Dan G. Sep 28 '12 at 01:23
  • 1
    @DanG. the question and answers are going to remain on the site forever. I didn't provide this answer for your benefit, but for the benefit of Googlers in the future who have a problem more similar to the title. – Mark Ransom Sep 28 '12 at 01:29
  • @MarkRansom i would sugesst this answer for a post titled "how to convert a number in python from scientific notation." not "how to to keep a number out of scientific notation in the first place" – Dan G. Sep 29 '12 at 02:22
  • Actually this answers my question which was sort of what the title says. I would prefer something more elegant but I guess there is no other way. 12*10**15 would also do but int looks better. – kon psych Oct 05 '14 at 01:04