2021年1月15日星期五

Why does python's dict update() method return a None Type object?

I'm trying to create a dictionary that can be used to apply a cipher to a letter. The dictionary maps every uppercase and lowercase letter to a character shifted down the alphabet by the input shift. The dictionary should have 52 keys of all the uppercase letters and all the lowercase letters only. This is a method built in a class.

Here is my code:

import string  def build_shift_dict(self, shift):      '''      shift (integer): the amount by which to shift every letter of the       alphabet. 0 <= shift < 26        Returns: a dictionary mapping a letter (string) to                another letter (string).       '''      dict1={}      dict2={}      alphabet_lowercase=string.ascii_lowercase      alphabet_uppercase=string.ascii_uppercase      for t in range(26):          if t>=26-shift:              dict1[alphabet_lowercase[t]]=alphabet_lowercase[abs((26-shift)-t)]          else:              dict1[alphabet_lowercase[t]]=alphabet_lowercase[t+shift]      for t in range(26):          if t>=26-shift:              dict2[alphabet_uppercase[t]]=alphabet_uppercase[abs((26-shift)-t)]          else:              dict2[alphabet_uppercase[t]]=alphabet_uppercase[t+shift]      dictionary=dict1.update(dict2)      return dictionary  

Although dict1 and dict2 are what I expected to get, the assignment to dictionary returns a None Type object. Is there anything wrong with the 'update' function?

https://stackoverflow.com/questions/65745960/why-does-pythons-dict-update-method-return-a-none-type-object January 16, 2021 at 10:35AM

没有评论:

发表评论