0

The code that has been attatched (i couldnt post it for some reason) produces the error message:
Traceback (most recent call last):
File "filename", line 36, in <module>
for i in range(len(bytesmessage)/7):
TypeError: 'float' object cannot be interpreted as an integer

In this line
for i in range(len(bytesmessage)/7):
The variable bytesmessage should always be multiples of seven because its binary. I tried to make a loop which loops the amount of 7 binary digits but its unable to because it's a float?

Could someone help me solve this as I cant see why it would not work?
Thanks for reading.

Edited by differentiation: had to reattatch

Attachments
def GetBinary(message):
    binarymessage = []
    for i in range(len(message)):
        binarymessage.append(bin(ord(message[i])))
        binarymessage[i] = binarymessage[i][2:len(binarymessage[i])]
    return binarymessage

def GetCharacters(message):
    print(message)
    charmessage = ""
    for item in message:
        charmessage += chr(int(item, 2))
    return charmessage

def PrintMessage(message):
    if isinstance(message, list):
        for item in message:
            print(item, end="")
        print("")
    if isinstance(message, int):
        print(str(message))
    if isinstance(message, str):
        print(message)
    
menu = ""
while menu.lower() != "3":
    menu = input("Encrypt[1]/Decrypt[2]/Exit[3]: ")
    if menu == "1":#encodes the text to binary
        message = input("Enter a string to encrypt: ")
        message = GetBinary(message)
        PrintMessage(message)
    if menu == "2": #decodes the binary to text
        message = []
        bytesmessage = input("Enter a string to decrypt: ")
        print(str(len(bytesmessage)))
        for i in range(len(bytesmessage)/7):
            message.append(bytesmessage[(bytesmessage[i*7]-7):(bytesmessage[i*7])])
        message = GetCharacters(message)
        PrintMessage(message)
2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by HiHe
0

Are you using Python2 or Python3?
'/' changed from an integer division to a float division.

Edited by HiHe

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.