Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Encryption Help Posted by jade_casey on 11 Feb 2006 at 4:08 PM
Hi. I'm new to this Python language and I've been doing this project for the last two weeks:

Write an n-stepped encryption program.

This is a little different from what I told you in class, so follow these instructions.


The user should be able to set the number of steps that the encoding algorithm uses.

The program should only encrypt upper-case letters, spaces and numbers (any other character in the message should remain unencrypted).
The user should create a text file with an unencrypted message. The program should create a second text file, which is the encrypted message.



Coding Steps:

1. write a function encrypt that takes in a string and a step and implements an n-step encryption algorithm, returning the result
2. write a load function, that reads in all the data in a file
3. write a write function, that writes a string to a file
4. Write a program flow based around these functions
5. Write a program, using functions 1 through 3, to accomplish the assignment.

AND I'M STILL EXPERIENCING PRBLEMS.This is what I've come up with so far:

def read():
a=open("news.txt","r")
text=a.read()
a.close
return text
print


def encrypt(string,n):
result=[]
for char in string:
x=ord(char)
x+=2
result.append(chr(x))
return str(result)

def write(string):
b=open("output.txt","w")
b.write(string)
b.close()
string=[]
write(string)
Can someone please help me. It's due on Monday.

Report
Re: Encryption Help Posted by infidel on 13 Feb 2006 at 11:05 AM
: Hi. I'm new to this Python language and I've been doing this project for the last two weeks:
:
: Write an n-stepped encryption program.
:
: This is a little different from what I told you in class, so follow these instructions.
:
:
: The user should be able to set the number of steps that the encoding algorithm uses.
:
: The program should only encrypt upper-case letters, spaces and numbers (any other character in the message should remain unencrypted).
: The user should create a text file with an unencrypted message. The program should create a second text file, which is the encrypted message.
:
:
:
: Coding Steps:
:
: 1. write a function encrypt that takes in a string and a step and implements an n-step encryption algorithm, returning the result
: 2. write a load function, that reads in all the data in a file
: 3. write a write function, that writes a string to a file
: 4. Write a program flow based around these functions
: 5. Write a program, using functions 1 through 3, to accomplish the assignment.
:
: AND I'M STILL EXPERIENCING PRBLEMS.This is what I've come up with so far:
:
: def read():
: a=open("news.txt","r")
: text=a.read()
: a.close
: return text
: print
:
:
: def encrypt(string,n):
: result=[]
: for char in string:
: x=ord(char)
: x+=2
: result.append(chr(x))
: return str(result)
:
: def write(string):
: b=open("output.txt","w")
: b.write(string)
: b.close()
: string=[]
: write(string)
: Can someone please help me. It's due on Monday.

Sorry I didn't get to this until it was "too late". You made a good effort, so I'm willing to show you how I would've done it:

import string
encryptable = string.ascii_uppercase + string.digits + string.whitespace

def read(path):
    f = open(path,"r")
    text = f.read()
    f.close()
    return text

def encrypt(s, step=1):
    result=[]
    for c in s:
        if c in encryptable:
            x = ord(c) + step
            result.append(chr(x))
        else:
            result.append(c)
    return ''.join(result)

def write(path, s):
    f = open(path,"w")
    f.write(s)
    f.close()


Let me know if you still need help or don't understand anything here, or if you'd like a more thorough analysis of anything.



infidel

$ select * from users where clue > 0
no rows returned


Report
Re: Encryption Help Posted by jade_casey on 15 Feb 2006 at 4:15 PM
: : Hi. I'm new to this Python language and I've been doing this project for the last two weeks:
: :
: : Write an n-stepped encryption program.
: :
: : This is a little different from what I told you in class, so follow these instructions.
: :
: :
: : The user should be able to set the number of steps that the encoding algorithm uses.
: :
: : The program should only encrypt upper-case letters, spaces and numbers (any other character in the message should remain unencrypted).
: : The user should create a text file with an unencrypted message. The program should create a second text file, which is the encrypted message.
: :
: :
: :
: : Coding Steps:
: :
: : 1. write a function encrypt that takes in a string and a step and implements an n-step encryption algorithm, returning the result
: : 2. write a load function, that reads in all the data in a file
: : 3. write a write function, that writes a string to a file
: : 4. Write a program flow based around these functions
: : 5. Write a program, using functions 1 through 3, to accomplish the assignment.
: :
: : AND I'M STILL EXPERIENCING PRBLEMS.This is what I've come up with so far:
: :
: : def read():
: : a=open("news.txt","r")
: : text=a.read()
: : a.close
: : return text
: : print
: :
: :
: : def encrypt(string,n):
: : result=[]
: : for char in string:
: : x=ord(char)
: : x+=2
: : result.append(chr(x))
: : return str(result)
: :
: : def write(string):
: : b=open("output.txt","w")
: : b.write(string)
: : b.close()
: : string=[]
: : write(string)
: : Can someone please help me. It's due on Monday.
:
: Sorry I didn't get to this until it was "too late". You made a good effort, so I'm willing to show you how I would've done it:
:
:
: import string
: encryptable = string.ascii_uppercase + string.digits + string.whitespace
: 
: def read(path):
:     f = open(path,"r")
:     text = f.read()
:     f.close()
:     return text
: 
: def encrypt(s, step=1):
:     result=[]
:     for c in s:
:         if c in encryptable:
:             x = ord(c) + step
:             result.append(chr(x))
:         else:
:             result.append(c)
:     return ''.join(result)
: 
: def write(path, s):
:     f = open(path,"w")
:     f.write(s)
:     f.close()
: 

:
: Let me know if you still need help or don't understand anything here, or if you'd like a more thorough analysis of anything.
:
:
:
: infidel
:
:
: $ select * from users where clue > 0
: no rows returned
: 

:
: Thank you so much.I really do appreciate it.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.