Skip to main content
 首页 » 编程设计

python之SageMathCloud : Error: unexpected EOF while parsing

2024年10月25日13qq号

在 SageMathCloud 的 Sage 工作表(.sagews 文件)中,此代码:

def elgamal_encrypt ( pub_key ,g ,p , message ): 
k = floor ( 1+( p -2)* random ()) 
return ( Mod (g , p )^ k , message * Mod ( pub_key ^k , p ) ) 

产生错误:

Error in lines 1-1 
 
Traceback (most recent call last): 
  File "/projects/7870d70a-93d1-44f4-bce4-990a540707b7/.sagemathcloud/sage_server.py", line 879, in execute 
    exec compile(block+'\n', '', 'single') in namespace, locals 
 
  File "<string>", line 1 
 
    def elgamal_encrypt ( pub_key ,g ,p , message ): 
                                                   ^ 
SyntaxError: unexpected EOF while parsing 

如何纠正?

请您参考如下方法:

缩进 k=... 行。

def elgamal_encrypt ( pub_key ,g ,p , message ): 
    k = floor ( 1+( p -2)* random ()) 
    return ( Mod (g , p )^ k , message * Mod ( pub_key ^k , p ) ) 

请注意,这发生在您身上的特定原因是因为您在 SageMathCloud 上——参见 this page有关错误和修复的详细信息。 (感谢@Samuel Lelièvre 的评论。)