python - AttributeError: 'str' object has no attribute 'build_shift_dict'? -


i solving quiz requires write code generate ciphered text, depends on simple algorithm shift characters number of steps toward right. here code , don't know error, i'm trying find 'str' in code can't find ever !! can tell me wrong code , how fix run ?

import string   ### not modify function ### def load_words(file_name):     '''     file_name (string): name of file containing      list of words load          returns: list of valid words. words strings of lowercase letters.      depending on size of word list, function may     take while finish.     '''     print('loading word list file...')     # infile: file     in_file = open(file_name, 'r')     # line: string     line = in_file.readline()     # word_list: list of strings     word_list = line.split()     print('  ', len(word_list), 'words loaded.')     in_file.close()     return word_list   ### not modify function ### def is_word(word_list, word):     '''     determines if word valid word, ignoring     capitalization , punctuation      word_list (list): list of words in dictionary.     word (string): possible word.      returns: true if word in word_list, false otherwise      example:     >>> is_word(word_list, 'bat') returns     true     >>> is_word(word_list, 'asdf') returns     false     '''     word = word.lower()     word = word.strip(" !@#$%^&*()-_+={}[]|\:;'<>?,./\"")     return word in word_list   ### not modify function ### def get_story_string():     """     returns: joke in encrypted text.     """     f = open("story.txt", "r")     story = str(f.read())     f.close()     return story   wordlist_filename = 'words.txt'   class message(object):     ### not modify method ###     def __init__(self, text):         '''         initializes message object          text (string): message's text          message object has 2 attributes:             self.message_text (string, determined input text)             self.valid_words (list, determined using helper function load_words         '''         self.message_text = text         self.valid_words = load_words(wordlist_filename)      ### not modify method ###     def get_message_text(self):         '''         used safely access self.message_text outside of class          returns: self.message_text         '''         return self.message_text      ### not modify method ###     def get_valid_words(self):         '''         used safely access copy of self.valid_words outside of class          returns: copy of self.valid_words         '''         return self.valid_words[:]      def build_shift_dict(shift):         '''         creates dictionary can used apply cipher letter.         dictionary maps every uppercase , lowercase letter         character shifted down alphabet input shift. dictionary         should have 52 keys of uppercase letters , lowercase         letters only.          shift (integer): amount shift every letter of         alphabet. 0 <= shift < 26          returns: dictionary mapping letter (string)                  letter (string).         '''         assert 0 <= shift < 26         shifted_dict = {}          def shift_letters(shift, text):             in range(len(text)):                 if < shift:                     shifted_dict[text[i]] = text[26 - shift + i]                 else:                     shifted_dict[text[i]] = text[i - shift]          shift_letters(shift, string.ascii_lowercase)         shift_letters(shift, string.ascii_uppercase)         return shifted_dict      def apply_shift(self, shift):         '''         applies caesar cipher self.message_text input shift.         creates new string self.message_text shifted down         alphabet number of characters determined input shift                  shift (integer): shift encrypt message.         0 <= shift < 26          returns: message text (string) in every character shifted              down alphabet input shift         '''          shifted_dict = self.build_shift_dict(shift)         result = ""         in self.message_text:             if not in string.ascii_lowercase , not in string.ascii_uppercase:                 result +=             else:                 result += shifted_dict[i]         return result   class plaintextmessage(message):     def __init__(self, text, shift):         '''         initializes plaintextmessage object                  text (string): message's text         shift (integer): shift associated message          plaintextmessage object inherits message , has 5 attributes:             self.message_text (string, determined input text)             self.valid_words (list, determined using helper function load_words)             self.shift (integer, determined input shift)             self.encrypting_dict (dictionary, built using shift)             self.message_text_encrypted (string, created using shift)          hint: consider using parent class constructor less          code repeated         '''         self.text = text         self.shift = shift         self.encrypting_dict = message.build_shift_dict(shift)         self.message_text_encrypted = message.apply_shift(text, shift)      def get_shift(self):         '''         used safely access self.shift outside of class          returns: self.shift         '''         return self.shift      def get_encrypting_dict(self):         '''         used safely access copy self.encrypting_dict outside of class          returns: copy of self.encrypting_dict         '''         return self.encrypting_dict      def get_message_text_encrypted(self):         '''         used safely access self.message_text_encrypted outside of class          returns: self.message_text_encrypted         '''         return self.message_text_encrypted()      def change_shift(self, shift):         '''         changes self.shift of plaintextmessage , updates other          attributes determined shift (ie. self.encrypting_dict ,          message_text_encrypted).          shift (integer): new shift should associated message.         0 <= shift < 26          returns: nothing         '''         assert 0 <= shift < 26         self.shift = shift         self.encrypting_dict = message.build_shift_dict(shift)         self.message_text_encrypted = message.apply_shift(text, shift)   class ciphertextmessage(message):     def __init__(self, text):         '''         initializes ciphertextmessage object          text (string): message's text          ciphertextmessage object has 2 attributes:             self.message_text (string, determined input text)             self.valid_words (list, determined using helper function load_words)         '''         self.message = message      def decrypt_message(self):         '''         decrypt self.message_text trying every possible shift value         , find "best" one. define "best" shift         creates maximum number of real words when use apply_shift(shift)         on message text. if s original shift value used encrypt         message, expect 26 - s best shift value          decrypting it.          note: if multiple shifts  equally such create          maximum number of may choose of shifts (and         corresponding decrypted messages) return          returns: tuple of best shift value used decrypt message         , decrypted message text using shift value         '''         in range(26):             decrypted = message.apply_shift(self.message , i)             word in decrypted :                 if word in message.valid_words :                     return (i , decrypted)   # example test case (plaintextmessage) plaintext = plaintextmessage('hello', 2) print('expected output: jgnnq') print('actual output:', plaintext.get_message_text_encrypted())  # example test case (ciphertextmessage) ciphertext = ciphertextmessage('jgnnq') print('expected output:', (24, 'hello')) print('actual output:', ciphertext.decrypt_message()) 

i wrote every time run code gives me error !!??

traceback (most recent call last):   file "c:/users/ahmad galal/desktop/ps6/ps6.py", line 248, in <module>     plaintext = plaintextmessage('hello', 2)   file "c:/users/ahmad galal/desktop/ps6/ps6.py", line 168, in __init__     self.message_text_encrypted = message.apply_shift(text, shift)   file "c:/users/ahmad galal/desktop/ps6/ps6.py", line 137, in apply_shift     shifted_dict = eval(self.build_shift_dict(shift)) attributeerror: 'str' object has no attribute 'build_shift_dict'  process finished exit code 1 

def build_shift_dict(shift) means message instance not have build_shift_dict.
change def build_shift_dict(shift) def build_shift_dict(self, shift)


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -