SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2010-04-28 11:24:01

r00t
Member
Registered: 2010-04-26
Posts: 19

Python question

I know,it is not a programming language forum,but I did not want to register in another forum for just one little question.
Hope,that there is somebody who is able to help,the problem is the following one:
I am trying to code a little tool to encrypt a string using the Caesar Cipher (Link is to Wikipedia.org for those who don't know it!) and the basic code for the encryption itself is always something like that:

print "Please enter your word .."
print ".. that should be encrypted:"
phrase = input()
test = phrase.replace ( 'a', 'b')
print test

I always get a following error - warning when I try to run my programm:

Traceback (most recent call last):
  File "Caesar.py", line 4, in <module>
    phrase = input()
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined

I know that this is an absolutely "noobish" question and that I made an absolutely dumb mistakte - but I try to learn out of my mistakes.:)
Thank everybody for his / her help.

cheers!


My blog (Sorry,it is written in German!)
Acer Aspire One A150 | Intel Atom N270 | 512 MB DDR2 RAM | Intel GMA | 8GB SSD | Crunchbang 09.04

Offline

Help fund CrunchBang, donate to the project!

#2 2010-04-28 11:57:13

RJB
Member
From: in /home
Registered: 2010-04-19
Posts: 11

Re: Python question

Hi there r00t,

I'm a noob at python myself, but I tinkered with your code (hope you don't mind smile ) and this works for me:

#!/usr/bin/python

print "Please enter your word ..";
print ".. that should be encrypted:";
phrase = str(raw_input());
test = phrase.replace ('a', 'b');
print test;

I changed the input line from "phrase = input()" to

phrase = str(raw_input());

Basically I only declare phrase as a string, as the replace() function only works on strings..


Thanks for everything Linux has taught, is teaching, and will teach me...

Offline

#3 2010-04-28 13:40:32

r00t
Member
Registered: 2010-04-26
Posts: 19

Re: Python question

Nah,I absolutely don't mind - it is just about getting into the language so I am challenging myself with some little tools! smile
I think that the declaration of phrase (Just a random name,will change it later) was the main problem .. I'll try and reedit my post,thanks for your help!

cheers!


My blog (Sorry,it is written in German!)
Acer Aspire One A150 | Intel Atom N270 | 512 MB DDR2 RAM | Intel GMA | 8GB SSD | Crunchbang 09.04

Offline

#4 2010-04-28 15:57:58

safetycopy
urban legend
From: The Chatsubo
Registered: 2010-04-03
Posts: 1,311

Re: Python question

You're getting an error with input() because input() expects an expression to evaluate. raw_input() only expects a string. You can see what I mean with this script:

#! /usr/bin/env python

test = input("--> ")
print test

If you run that script and enter '2+2' at the prompt, the output will be 4 as the expression has been evaluated rather than just printed.

I had a quick stab at your Caesar Cipher script and came up with this:

#! /usr/bin/env python

# Get input from user
plaintext = raw_input("Phrase to encrypt: ")
# Create new string to hold encrypted text
ciphertext = ""
# Loop through each character in user input
for i in plaintext:
        # Convert character to it's ascii number
        c = ord(i)
        # If character is Z (90), change it to A (65)
        if c == 90:
                c = 65
        # If character is z (122), change it to a (97)
        elif c == 122:
                c = 97
        # Otherwise, add 1 to the ascii code (97,'a', becomes 98, 'b')
        else:
                c += 1
        # Convert new number back to a letter and add to ciphertext
        ciphertext += chr(c)
# Display encrypted string
print "Encrypted phrase: " + ciphertext

Hope that helps! :-)


i wonder if i missed the warning
Skinny Puppy, Love in Vein

Offline

#5 2010-04-29 18:01:00

r00t
Member
Registered: 2010-04-26
Posts: 19

Re: Python question

Thanks again for your tips,really appreciate!
I will work through the code in the next week,it may take some time because my final exams are in the next week so at the moment,they are more important than my Python - Skills! wink

cheers!


My blog (Sorry,it is written in German!)
Acer Aspire One A150 | Intel Atom N270 | 512 MB DDR2 RAM | Intel GMA | 8GB SSD | Crunchbang 09.04

Offline

#6 2010-04-29 18:06:31

safetycopy
urban legend
From: The Chatsubo
Registered: 2010-04-03
Posts: 1,311

Re: Python question

No probs and good luck with your exams! :-)


i wonder if i missed the warning
Skinny Puppy, Love in Vein

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo