user1875892 user1875892 - 7 months ago 40
Python Question

How can I get my encoded number onto a new line with easygui?

I want to get the

onto a new line under "Heres the number in base 64: ", does anyone know a way?

import easygui as e

nte = e.enterbox(msg="Enter Number to convert", title="", default="", strip=True)
ntei = int(nte)

def base64encode(number, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"):
"""Converts an integer to a base64 string."""
if not isinstance(number, (int, long)):
raise TypeError("Number must be an integer")

base64 = ""
sign = ""

if number < 0:
sign = "_"
number = -number

if 0 <= number <len(alphabet):
return sign + alphabet[number]

while number !=0:
number, i = divmod(number, len(alphabet))
base64 = alphabet[i] + base64

return sign + base64

def base60decode(number):
return int(number, 64)

print e.msgbox(msg=("Heres the number in base 64: " + base64encode(ntei)))


The problem is with "Heres the number in base 64: " + base64encode(ntei). You are not putting a newline there yourself, so naturally it doesn't display one. Newlines are represented via the character \n in Python. So to add a newline to that string, we must change it to "Heres the number in base 64:\n" + base64encode(ntei).

It is also worth mentioning that your base64encode function is completely unnecessary (as is using print in front of easygui.msgbox, unless for some reason you wanted to see that function's return value). You can convert a string to base64 via the encode method of Python strings, as in ntei.encode('base64'). Using this knowledge, your entire program can be rewritten to the following:

import easygui as e

nte = e.enterbox(msg='Enter number to convert', title='', default='', strip=True)
e.msgbox(msg=('Heres the number in base 64: ' + nte.encode('base64')))