为什么这段代码打印构造函数?


class CipherTest:
def __init__(self):
self.shift = 0
self.direction = 'r'
self.text = "Testing"

# Shift to right function
def shift_to_right(self, text, shift):
encrypted_text = ""
for i in range(len(self.text)):
c = self.text[i]
# Encrypt upper case
if (c == ' '):
encrypted_text += ' '
elif (c.isupper()):
encrypted_text += chr((ord(c) + self.shift - 65) % 26 + 65)
# Encrypt lower case
else:
encrypted_text += chr((ord(c) + self.shift - 97) % 26 + 97)

return encrypted_text

# Shift to left function
def shift_to_left(self, text, shift):
encrypted_text = ""
for i in range(len(self.text)):
c = self.text[i]
# Encrypt upper case
if (c == ' '):
encrypted_text += ' '
elif (c.isupper()):
encrypted_text += chr((ord(c) - self.shift - 65) % 26 + 65)
# Encrypt lower case
else:
encrypted_text += chr((ord(c) - self.shift - 97) % 26 + 97)

return encrypted_text

if __name__ == "__main__":
user_text = str(input())
user_shift = int(input())
user_direction = str(input().lower())  # user inputs
Cipher_Message = CipherTest()  # create an instance of the class
if user_direction == 'l':  # picking left or right 
print(CipherTest.shift_to_left(Cipher_Message, user_text, user_shift))

if user_direction == 'r':
print(CipherTest.shift_to_right(Cipher_Message, user_text, user_shift))

我调用函数在我的类不正确吗?目前,无论输入什么,它都打印"testing"。我是新的类,但我相信,在最后几行,我调用新的实例,告诉它哪个方法来执行,然后提供它需要成功运行的变量。从那里,它应该打印使用我的方法返回的'encrypted_text'。

问题是CipherTest有属性方向,移位和文本,但你也有局部变量,你传递作为参数,你不使用

所以你的方法继续使用Testing0,返回Testing所以


最简单的,是删除属性,这样类实例可以做两个方向,实际上方法可以是静态的(意味着不需要实例,只需要类)

class CipherTest:
@staticmethod
def shift_to_right(text, shift):
encrypted_text = ""
for c in text:
if c == ' ':
encrypted_text += ' '
elif c.isupper():
encrypted_text += chr((ord(c) + shift - 65) % 26 + 65)
else:
encrypted_text += chr((ord(c) + shift - 97) % 26 + 97)
return encrypted_text
if __name__ == "__main__":
user_text = "hey how are you"
user_shift = 2
user_direction = "l"
if user_direction == 'l':
enc = CipherTest.shift_to_left(user_text, user_shift)
print(enc)  # encrypted
print(CipherTest.shift_to_right(enc, user_shift))  # back to plain
elif user_direction == 'r':
enc = CipherTest.shift_to_right(user_text, user_shift)
print(enc)  # encrypted
print(CipherTest.shift_to_left(enc, user_shift))  # back to plain

另一个例子,让你更好地理解,是把shift作为类属性,为了使用ise,我们需要在类中保存self.shift = shift,然后我们可以使用一个密码实例,用它的移位,做这两个操作(也看看我们如何真正调用实例方法)

class CipherTest:
def __init__(self, shift: int):
self.shift = shift
def shift_to_right(self, text):
encrypted_text = ""
for c in text:
if c == ' ':
encrypted_text += ' '
elif c.isupper():
encrypted_text += chr((ord(c) + self.shift - 65) % 26 + 65)
else:
encrypted_text += chr((ord(c) + self.shift - 97) % 26 + 97)
return encrypted_text
if __name__ == "__main__":
user_text = "hey how are you"
user_shift = 2
user_direction = "l"
cipher = CipherTest(user_shift)
if user_direction == 'l':
enc = cipher.shift_to_left(user_text)
print(enc)  # encrypted
print(cipher.shift_to_right(enc))  # back to plain
elif user_direction == 'r':
enc = cipher.shift_to_right(user_text)
print(enc)  # encrypted
print(cipher.shift_to_left(enc))  # back to plain

将类作为参数传递给函数。CipherTest.shift_to_left(Cipher_Message, user_text, user_shift)

当你在python中定义一个类方法时,你将self作为参数包含,但在调用函数时并不实际传递它。它将Cipher_Message(实例)解释为您打算作为user_text输入的内容。

通过简单地从你的参数中删除Cipher_Message来修复这个问题:CipherTest.shift_to_left(user_text, user_shift)

你还没做完呢。您正在尝试调用类上的非静态方法,而不是实例。这意味着您告诉"蓝图"来转移加密,而不是实际的测试类。通过将类更改为实例来修复此问题:Cipher_Message.shift_to_left(user_text, user_shift)

你还有一个不可破坏的bug,你的函数似乎接受了你没有使用的参数。def shift_to_right(self, text, shift):有两个参数:text &Shift,但您使用类的字段而不是那些。

只要从定义和调用中删除它们就可以解决这个问题。def shift_to_right(self):.shift_to_right()

在您的类方法中,您接受text和shift的参数,但是方法的代码从不引用这些参数。这些方法只引用实例变量self。文本和自我。shift,这是实例Cipher_Message的属性,在创建新实例时执行__init__时分配

两个选择:

  1. 更新方法以使用参数而不是实例属性。
  2. 更新__init__以text和shift作为参数,并根据传入的参数分配实例属性。

忽略如何处理用户输入,这可以大大简化。

class CipherTest:
@staticmethod
def shift_to_left(text, shift):
return CipherTest.shift(text, -shift)
@staticmethod
def shift_to_right(text, shift):
return CipherTest.shift(text, shift)
@staticmethod
def shift(text, shift):
result = ''
for c in text:
if c.isupper():
c = chr((ord(c) + shift - 65) % 26 + 65)
elif c.islower():
c = chr((ord(c) + shift - 97) % 26 + 97)
# note that whitespace is neither upper nor lower so just append unchanged
result += c
return result
print(CipherTest.shift_to_left(CipherTest.shift_to_right('Hello world', 3), 3))

…这使界面保持相似。另外:

print(CipherTest.shift(CipherTest.shift('Hello world', 3), -3))

输出:

Hello world

最新更新