所有方法都需要缩进一级,以指示它们是python中类的方法

2024-09-30 18:32:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Wiki上遵循RSA算法:http://en.wikipedia.org/wiki/RSA_(algorithm)

我使用的是python3.3.0,我正在尝试RSA加密,但遇到了两个我不知道怎么做的问题。你知道吗

在Encryptions类中,我的方法都需要缩进一级,以表明它们是该类的方法,而不是全局函数。你知道吗

当主脚本在最后请求输入时,如果我只是按return,就会抛出一个异常,说明Python达到了一个意外的EOF。你知道吗

我该怎么做?你知道吗

到目前为止我的代码是:

你知道吗模块化.py你知道吗

def _base_b_convert(n, b):
   if b < 1 or n < 0:
      raise ValueError("Invalid Argument")

   q = n
   a = []
   while q != 0:
      value = int(q % b)
      a.append(value)
      q =int(q / b)
   return a


def mod_exp(base, n, mod):
    if base < 0 or n < 0 or mod < 0:
    raise ValueError("Invalid Argument")
    a = (_base_b_convert(n, 2))
    x = 1
    pow = base % mod
    for i in range(0, len(a)):
    if a[i] == 1:
        x = (x * pow) % mod
    pow = pow**2 % mod
    return x

你知道吗主.py你知道吗

from encryptions import Encryptions

def main():
    enc = Encryptions()
    message = enc.encrypt(message)
    print(message)
    print()
    print("Decrypting message:")
    message = enc.decrypt(message)
    print(message)

    input("--Press any key to end--")

if __name__ == '__main__':
    main()

Tags: or方法pymodmessagebasereturnif
2条回答

你的压痕消失了。有时你有3个空格,有时4个,有时5个。你知道吗

另一个例子在这里

def mod_exp(base, n, mod):
    if base < 0 or n < 0 or mod < 0:
    raise ValueError("Invalid Argument")
    a = (_base_b_convert(n, 2))
    x = 1
    pow = base % mod
    for i in range(0, len(a)):
    if a[i] == 1:
        x = (x * pow) % mod
    pow = pow**2 % mod
    return x

看起来应该更像

def mod_exp(base, n, mod):
    if base < 0 or n < 0 or mod < 0:
        raise ValueError("Invalid Argument")
    a = (_base_b_convert(n, 2))
    x = 1
    pow = base % mod
    for i in range(0, len(a)):
        if a[i] == 1:
            x = (x * pow) % mod
        pow = pow**2 % mod
    return x

无论何时使用if、while、for等,都需要缩进一级。你知道吗

(这些问题可能只是因为它严重复制到了stackoverflow?)你知道吗

input()在python2中并不是您所想的那样,而是它计算作为Python代码输入的字符串,这不是您所想要的。相反,使用raw_input。你知道吗

至于缩进问题,那只是Python语法。你无能为力。你知道吗

相关问题 更多 >