1

I need to encrypt a file with a key phrase for part of an assessment. I am doing mine in python and have run into a problem. It is written using python 2.7.4

My code is as follows:

import array

def encrypter(intext, shift, modder):
    plain2 = list(intext)
    plain = array.fromlist(plain2)
    out = ''
    j = 0
    key = list(shift)
    for c in plain:
        if mod > 0:
            x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        if mod < 0:
            x = chr((ord(c) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        out += x
        j += 1
    return out
sel = raw_input("Encrypt (e)/ Decrypt (d)")
if sel == 'e':
    mod = 1
    intext = open(raw_input("what is your file"),'r')
    shift = raw_input("what is your first password")
    encrypter(intext, shift, mod)

else:
    pass

My problem is that whenever I run this with a file called text1.txt I get this error:

Traceback (most recent call last):
  File "D:/Programming/Computing GCSE/Tasks/task3.py", line 22, in <module>
    encrypter(intext, shift, mod)
  File "D:/Programming/Computing GCSE/Tasks/task3.py", line 5, in encrypter
    plain = array.fromlist(plain2)
AttributeError: 'module' object has no attribute 'fromlist'

Can anyone suggest a change in my code? I need this relatively quickly as my assessment is in an hour or so!

1
  • 1
    If this work is important you needed to start it sooner, before it became urgent. It's urgent for you now, not for us. Commented May 7, 2015 at 16:02

2 Answers 2

2

It's not clear why you need the array module at all. Won't something like this work?

def encrypter(intext, shift, modder):
    plain = intext
    out = ''
    j = 0
    key = shift
    for c in plain:
        if mod > 0:
            x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        if mod < 0:
            x = chr((ord(c) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        out += x
        j += 1
    return out
Sign up to request clarification or add additional context in comments.

4 Comments

#Traceback (most recent call last): File "D:/Programming/Computing GCSE/Tasks/task3.py", line 22, in <module> encrypter(intext, shift, mod) File "D:/Programming/Computing GCSE/Tasks/task3.py", line 5, in encrypter plain = array.array.fromlist(plain2) TypeError: descriptor 'fromlist' requires a 'array.array' object but received a 'list'
You probably don't even need the array module. See my edit.
when I do that I get this error Traceback (most recent call last): File "D:/Programming/Computing GCSE/Tasks/task3.py", line 22, in <module> encrypter(intext, shift, mod) File "D:/Programming/Computing GCSE/Tasks/task3.py", line 10, in encrypter x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48) TypeError: ord() expected a character, but string of length 15 found
Write intext = open(raw_input("what is your file"),'r').read().
1

import array class from module

from array import array
plain = array('b', plain2)

1 Comment

Traceback (most recent call last): File "D:/Programming/Computing GCSE/Tasks/task3.py", line 22, in <module> encrypter(intext, shift, mod) File "D:/Programming/Computing GCSE/Tasks/task3.py", line 5, in encrypter plain = array.fromlist(plain2) TypeError: descriptor 'fromlist' requires a 'array.array' object but received a 'list'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.