Need help with Python code

This is the place for queries that don't fit in any of the other categories.

Need help with Python code

Postby EnragedTadpole » Thu Apr 11, 2013 10:04 am

Hi there,

I am currently doing an assignment for university. I am required to make a program that will act as a Caesar Cipher. All has been going smoothly until for some unknown reason, I am getting a syntax error.

I have uploaded my Python program to this link http://rapidshare.com/files/1316243420/burtm008_encryptor.py and would appreciate it if someone could run the program, and try to identify why the error is occurring. I have no idea what I am doing wrong!

I will love anyone who helps me! Thanks!
EnragedTadpole
 
Posts: 4
Joined: Thu Apr 11, 2013 10:01 am

Re: Need help with Python code

Postby stranac » Thu Apr 11, 2013 10:26 am

Rapidshare wrote:Download not available
The download for this file has not been enabled. Only the user who uploaded it, can enable the download option.


Anyway, it's better to just post your code here, in code tags.
Read this if you don' know how.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need help with Python code

Postby EnragedTadpole » Thu Apr 11, 2013 10:51 am

I suppose I can, but I will have to post the full program because the issue is not indicated within a few lines, well.. From what I can tell.
I can tell you where the issue is occurring though.
EnragedTadpole
 
Posts: 4
Joined: Thu Apr 11, 2013 10:01 am

Re: Need help with Python code

Postby stranac » Thu Apr 11, 2013 10:53 am

Yeah, you should post the entire error traceback python gives you, along with the code.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need help with Python code

Postby EnragedTadpole » Thu Apr 11, 2013 10:55 am

Well, apparently there is a syntax error. I have highlighted where python identifies it but I do not understand:
Code: Select all
import string

# Prints the menu to the screen
print("*** Menu *** \n")

print("1. Encrypt string")
print("2. Decrypt string")
print("3. Brute force decryption")
print("4. Quit \n")

# Prompts the user for their selection
selection = int(input("What would you like to do [1,2,3,4]? "))

# Checks for input error
while selection < 1 or selection > 4:
   
    print("\nThis is not an option.")
    selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 1
while selection == 1:
       
    # Prints to the screen the chosen option and prompts the user to input
    print("In command 1 - encrypt string \n")
    code = input("Please enter string to encrypt: ")
    offset = int(input("Please enter positive offset value: "))
    print("")
   
    # Checks for input error
    while offset > 26 or offset < 1:
        offset = int(input("Please enter a positive offset value between 1 - 26: "))

    # Assigns the 'wholeEncryption' variable to keep track of each
    # letter which is encrypted
    wholeEncryption = ""

    # Loops the input in variable 'code' for each letter
    for letter in range(len(code)):

        # Converts each letter in 'code' to an ASCII value
        # and adds the positive offset value
        encryption = ord(code[letter])
        encryption += offset

        # Loops the ASCII value to beginning if True
        if encryption > 126:
            encryption -= 94

        # Converts the letter back into a string and adds it to the total string
        encryption = chr(encryption)
        wholeEncryption += encryption
   
    # Prints the entire encrypted string to the screen
    print("\nEncrypted string:")
    print(wholeEncryption)

    # Prints the menu to the screen
    print("\n*** Menu *** \n")

    print("1. Encrypt string")
    print("2. Decrypt string")
    print("3. Brute force decryption")
    print("4. Quit \n")

    # Prompts the user for their selection
    selection = int(input("What would you like to do [1,2,3,4]? "))

    # Checks for input error
    while selection < 1 or selection > 4:
   
        print("\nThis is not an option.")
        selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 2
while selection == 2:
   
    # Prints to the screen the chosen option and prompts the user to input
    print("In command 2 - decrypt string \n")
    code = input("Please enter string to decrypt: ")
    offset = int(input("Please enter negative offset value: "))
    print("")

    # Checks for input error
    while offset < -26 or offset > -1:
        offset = int(input("Please enter negative offset value between -1 - -26: "))

    # Assigns the 'wholeDecryption' variable to keep track of each
    # letter which is decrypted
    wholeDecryption = ""

    # Loops the input in variable 'code' for each letter
    for letter in range(len(code)):

        # Converts each letter in 'code' to an ASCII value
        # and adds the negative offset value
        decryption = ord(code[letter])
        decryption += offset

        # Loops the ASCII value to beginning if True
        if decryption < 32:
            decryption += 94

        # Converts the letter back into a string and adds it to the total string
        decryption = chr(decryption)
        wholeDecryption += decryption
   
    # Prints the entire decrypted string to the screen
    print("\nDecrypted string:")
    print(wholeDecryption)

    # Prints the menu to the screen
    print("\n*** Menu *** \n")

    print("1. Encrypt string")
    print("2. Decrypt string")
    print("3. Brute force decryption")
    print("4. Quit \n")

    # Prompts the user for their selection
    selection = int(input("What would you like to do [1,2,3,4]? "))

    # Checks for input error
    while selection < 1 or selection > 4:
   
        print("\nThis is not an option.")
        selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 3
while selection == 3:

    # Initialises loop control
    offset = 0
    print("In command 3 - brute force decryption \n")
    code = input("Please enter string to decrypt: "
                 
    # Loops the 'for loop' until offset value is less than -26
    while offset <= (-26):                                                        <======= syntax error here apparently ***************************

        # Loops the input in variable 'code' for each letter
        for letter in range(len(code)):

            # Update loop control
            offset -= 1

            # Converts each letter in 'code' to an ASCII value
            # and adds the negative offset value
            decryption = ord(code[letter])
            decryption += offset

            # Loops the ASCII value to beginning if True
            if decryption < 32:
                decryption += 94

            # Converts the letter back into a string and adds it to the total string
            decryption = chr(decryption)
            wholeDecryption += decryption
   
        # Prints the entire decrypted string to the screen
        print("\nOffset:", offset, "= Decrypted string:", wholeDecryption)

    # Prints the menu to the screen
    print("\n*** Menu *** \n")

    print("1. Encrypt string")
    print("2. Decrypt string")
    print("3. Brute force decryption")
    print("4. Quit \n")

    # Prompts the user for their selection
    selection = int(input("What would you like to do [1,2,3,4]? "))

    # Checks for input error
    while selection < 1 or selection > 4:
   
        print("\nThis is not an option.")
        selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 4
else:
   
    print("\nGoodbye.")
EnragedTadpole
 
Posts: 4
Joined: Thu Apr 11, 2013 10:01 am

Re: Need help with Python code

Postby stranac » Thu Apr 11, 2013 11:00 am

The error is on the line before.
Code: Select all
    code = input("Please enter string to decrypt: "

You're missing a closing parenthesis.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need help with Python code

Postby EnragedTadpole » Thu Apr 11, 2013 11:02 am

Oh, wow. Hahaha, omg. Thanks a ton!
I feel really stupid :roll:

EDIT: That actually helped me a lot! I made the same mistake on one of my other programs and after quickly scrolling through and just looking out for that I realised that I make the same mistake quite a few times. Luckily I'm only a beginner so I now know what to improve on. Thanks again! I appreciate it heaps! :)
EnragedTadpole
 
Posts: 4
Joined: Thu Apr 11, 2013 10:01 am

Re: Need help with Python code

Postby Kebap » Thu Apr 11, 2013 1:19 pm

EnragedTadpole wrote: Luckily I'm only a beginner so I now know what to improve on. Thanks again! I appreciate it heaps! :)

Maybe after that learn about python functions, so you do not have to copy the menu code many times, just because you want to use it many times. :ugeek:
EnragedTadpole wrote:
Code: Select all
import string

# Prints the menu to the screen
print("*** Menu *** \n")

print("1. Encrypt string")
print("2. Decrypt string")
print("3. Brute force decryption")
print("4. Quit \n")

# Prompts the user for their selection
selection = int(input("What would you like to do [1,2,3,4]? "))

# Checks for input error
while selection < 1 or selection > 4:
   
    print("\nThis is not an option.")
    selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 1
while selection == 1:
       
    # Prints to the screen the chosen option and prompts the user to input
    print("In command 1 - encrypt string \n")
    code = input("Please enter string to encrypt: ")
    offset = int(input("Please enter positive offset value: "))
    print("")
   
    # Checks for input error
    while offset > 26 or offset < 1:
        offset = int(input("Please enter a positive offset value between 1 - 26: "))

    # Assigns the 'wholeEncryption' variable to keep track of each
    # letter which is encrypted
    wholeEncryption = ""

    # Loops the input in variable 'code' for each letter
    for letter in range(len(code)):

        # Converts each letter in 'code' to an ASCII value
        # and adds the positive offset value
        encryption = ord(code[letter])
        encryption += offset

        # Loops the ASCII value to beginning if True
        if encryption > 126:
            encryption -= 94

        # Converts the letter back into a string and adds it to the total string
        encryption = chr(encryption)
        wholeEncryption += encryption
   
    # Prints the entire encrypted string to the screen
    print("\nEncrypted string:")
    print(wholeEncryption)

    # Prints the menu to the screen
    print("\n*** Menu *** \n")

    print("1. Encrypt string")
    print("2. Decrypt string")
    print("3. Brute force decryption")
    print("4. Quit \n")

    # Prompts the user for their selection
    selection = int(input("What would you like to do [1,2,3,4]? "))

    # Checks for input error
    while selection < 1 or selection > 4:
   
        print("\nThis is not an option.")
        selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 2
while selection == 2:
   
    # Prints to the screen the chosen option and prompts the user to input
    print("In command 2 - decrypt string \n")
    code = input("Please enter string to decrypt: ")
    offset = int(input("Please enter negative offset value: "))
    print("")

    # Checks for input error
    while offset < -26 or offset > -1:
        offset = int(input("Please enter negative offset value between -1 - -26: "))

    # Assigns the 'wholeDecryption' variable to keep track of each
    # letter which is decrypted
    wholeDecryption = ""

    # Loops the input in variable 'code' for each letter
    for letter in range(len(code)):

        # Converts each letter in 'code' to an ASCII value
        # and adds the negative offset value
        decryption = ord(code[letter])
        decryption += offset

        # Loops the ASCII value to beginning if True
        if decryption < 32:
            decryption += 94

        # Converts the letter back into a string and adds it to the total string
        decryption = chr(decryption)
        wholeDecryption += decryption
   
    # Prints the entire decrypted string to the screen
    print("\nDecrypted string:")
    print(wholeDecryption)

    # Prints the menu to the screen
    print("\n*** Menu *** \n")

    print("1. Encrypt string")
    print("2. Decrypt string")
    print("3. Brute force decryption")
    print("4. Quit \n")

    # Prompts the user for their selection
    selection = int(input("What would you like to do [1,2,3,4]? "))

    # Checks for input error
    while selection < 1 or selection > 4:
   
        print("\nThis is not an option.")
        selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 3
while selection == 3:

    # Initialises loop control
    offset = 0
    print("In command 3 - brute force decryption \n")
    code = input("Please enter string to decrypt: "
                 
    # Loops the 'for loop' until offset value is less than -26
    while offset <= (-26):                                                        <======= syntax error here apparently ***************************

        # Loops the input in variable 'code' for each letter
        for letter in range(len(code)):

            # Update loop control
            offset -= 1

            # Converts each letter in 'code' to an ASCII value
            # and adds the negative offset value
            decryption = ord(code[letter])
            decryption += offset

            # Loops the ASCII value to beginning if True
            if decryption < 32:
                decryption += 94

            # Converts the letter back into a string and adds it to the total string
            decryption = chr(decryption)
            wholeDecryption += decryption
   
        # Prints the entire decrypted string to the screen
        print("\nOffset:", offset, "= Decrypted string:", wholeDecryption)

    # Prints the menu to the screen
    print("\n*** Menu *** \n")

    print("1. Encrypt string")
    print("2. Decrypt string")
    print("3. Brute force decryption")
    print("4. Quit \n")

    # Prompts the user for their selection
    selection = int(input("What would you like to do [1,2,3,4]? "))

    # Checks for input error
    while selection < 1 or selection > 4:
   
        print("\nThis is not an option.")
        selection = int(input("What would you like to do [1,2,3,4]? "))

# Evaluates to true if the user's selection equals 4
else:
   
    print("\nGoodbye.")
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 377
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests