Reputation: 141
I am trying to create a webpage which uses an interactive Sage cell to implement the Vigenere Cipher on user-inputted strings. Code runs perfectly when I run it outside of the interactive cell. See below:
message = 'Beware the Jabberwock, my son!'
key = 'VIGENERECIPHER'
from itertools import starmap, cycle
def encrypt(message, key):
message = filter(lambda _: _.isalpha(), message.upper())
def enc(c,k): return chr(((ord(k) + ord(c)) % 26) + ord('A'))
return "".join(starmap(enc, zip(message, cycle(key))))
encr = encrypt(message, key)
print encr
But when I try to implement it within the interactive cell, I get syntax errors.
@interact
def f(message = input_box('Beware the Jabberwock, my son!', label ="Plain text"), key = input_box('VIGENERECIPHER', label = "Key word")):
from itertools import starmap, cycle
def encrypt(message, key):
message = filter(lambda _: _.isalpha(), message.upper())
def enc(c,k): return chr(((ord(k) + ord(c)) % 26) + ord('A'))
return "".join(starmap(enc, zip(message, cycle(key))))
encr = encrypt(message, key)
print encr
The following error is printed:
AttributeError: 'exceptions.SyntaxError' object has no attribute 'upper'
I am new to python/sage... I'm guessing this is some sort of error with class/type? I've tried googling, but I can't find anything related to this problem specifically. Thanks
Upvotes: 1
Views: 72
Reputation: 1696
I don't see this AttributeError
, but another error instead. Maybe it's a sympton of the same thing. In any case, the problem is that message=input_box(...)
expects a Python expression in the box. You should add a type
keyword:
message=input_box('Beware the Jabberwock, my son!', label ="Plain text", type=str)
(Alternatively, you can enter all of your strings in the input box with explicit quotes.)
Upvotes: 2