Hi guys. This is my first post here and I've read through the forum quite a bit and can't wait to get more involved. So here is my main problem: getting my code to take the text I enter into an entry box to be recognized as a string so that I can continue to debug my program and finish it! I'll first paste the code and then show you the error message received as well. The two functions you see are all I can use (unless I want to add a decipher function for extra cred, which I do! Any help w/ that is appreciated).

I could use help for this as well:
Lines 27 and 31: my attempt at making my entry text into a str (didn't work)
Lines 44 and 45: just making sure my code works without using the GUI
Lines 46 - 48: trying to remove spaces and capitalize all letters of "message" and "key" but it does not work, therefore I went ahead and made them all caps/no spaces (for checking and debugging)
Line 49: Trying to make the keyword repeat to make it at least as long as the message to encode (but this was a failure as well)
Line 57: Why does this not undraw the rectangle?

Getting me headed in the right direction would do wonders!! Thanks so much.

-Harry

## vigenere.py


from graphics import *

def encode(message, key):
    
    str.upper(message)
    str.upper(key)
    str.replace(message, " ", "")

    encodedMessage = ""
    for i in range(len(message)):
        newVal = ord(key[i]) - ord("A")
        letterVal = newVal + ord(message[i])
        newLetter = chr(letterVal)
        encodedMessage = encodedMessage + newLetter
    return (encodedMessage)

        
def main():

    # GUI
    win = GraphWin("Vigenere Cipher", 600, 300)
    Text(Point(75,50), "Message to Encode").draw(win)
    message = Entry(Point(309, 50), 45)
##    message.setText(string)
    message.draw(win)
    Text(Point(89,80), "Enter Keyword").draw(win)
    key = Entry(Point(216, 80), 18)
##    key.setText(string)
    key.draw(win)
    button = Text(Point(120, 135), "Encode")
    button.draw(win)
    Rectangle(Point(80,155), Point(158, 110)).draw(win)
    Text(Point(76, 188), "Encoded Message").draw(win)
    output = Entry(Point(309, 188), 45)
    output.draw(win)
    # wait for mouse click
    win.getMouse()

    # encode phrase
    
##    message = "ATTACKATDAWN"
##    key = "CABCABCABCABCABCABCABCAB"
##    str.replace(message, " ", "")
##    str.upper(message)
##    str.upper(key)
##    key = key1 * (len(message)//(len(key1) + 1))
    print(message, key)
    newMessage = encode(message, key)
    print(newMessage)

    # display output and remove button
    output.setText(newMessage)
    # wait for mouse click
    #Rectangle.undraw()
    button.undraw()
    Text(Point(300, 285), "Click to Close Window").draw(win)
    win.getMouse()
    # click to quit
    win.close()
    ## end of main
main()

Recommended Answers

All 6 Replies

Line 27, string is never defined, and you should not use "string" as a variable. That name is already used by Python.

message_text = "Message Box"
    message.setText(message_text)

At line 46, etc. use a variable to store the changes.

message = "attackatdawn"
    cap_str = message.upper()
    print message, message.upper()

Line 49: Trying to make the keyword repeat to make it at least as long as the message to encode (but this was a failure as well)

No idea what "this was a failure as well" means.

Line 57: Why does this not undraw the rectangle?

No idea what the graphics module uses (no one really uses graphics), but generally you have to use a variable to point to the object's location in memory, and then tell the program which object/memory location to undraw.

withdraw_rec = Rectangle(Point(80,155), Point(158, 110)).draw(win)
    dont_withdraw_rec = Rectangle(Point(180,55), Point(258, 10)).draw(win)
    withdraw_rec.undraw()

thanks

for line 49: I simply meant that the code in line 49 did not do what I wanted it to do: repeat the keyword as many times as needed until it was as long as (or longer than) the message to encode.

for line 57: I tried to define my rectangle and then undraw it with the defined name and it still did not work. I use graphics because I'm instructed by my professor to do so.

thanks

for line 49: I simply meant that the code in line 49 did not do what I wanted it to do: repeat the keyword as many times as needed until it was as long as (or longer than) the message to encode.

for line 57: I tried to define my rectangle and then undraw it with the defined name and it still did not work. Edit: got it, I needed to leave off the .draw(win) from line 35 and draw it by doing rec.draw(win) which later allowed me to do the rec.undraw()

Well, I have everything solved except for the length of the keyword. I can not figure out how to get it to repeat itself enough times to satisfy the length of the message to encode. Any tips appreciated.

key = key1 * (len(message)//(len(key1) + 1))

key1 is never defined anywhere. I think you meant something like:

key = key * (len(message)//(len(key) + 1))

although it's impossible to tell what you want from "it doesn't work". Note that the code as posted will yield a zero since "key" is longer than "message". Also, check the arithmetic

len(message)//(len(key1) + 1))
key1 -->
len(message
//
len(key)+1

In the future, include the error message as you won't get any responses to "it doesn't work".

I had key1 defined elsewhere before (and when I changed it back I forgot to also change it accordingly throughout the code).

So the solution to that problem is, if you are wondering:

key = key * (len(message)//len(key)) + (key[0:len(message) % len(key)])

Also, I'll be sure to post error messages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.