旋转字符串中的字符时保留大写

时间:2018-08-31 19:40:21

标签: python

我需要编辑我的Rotate函数以考虑大写字母,我对此却感到困惑

下面是我的Python代码:

for(int i=0;i<30;++i){
    //System.out.println("task 1");
    try {
        new AlienReader("222.222", 22, "username","password");
    } catch (UnknownHostException | AlienReaderException | InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try{
        Thread.sleep(1000);
    }catch(Exception ignored){
}

但是返回以下错误

✖︎对于vigenere.encrypt('航行<3艘船穿过br0ken港口'

from helper import alphabet_position, rotate

    def encrypt(text,key):
        #Declare variable
        cipher = ''

        #Compute length
        l = len(key)

        #Assign value
        idx = 0

        #Loop
        for i in text:
            #if condition satisfies
            if i.isalpha():

                #Call method
                cipher += rotate(i,alphabet_position(key[idx]))

                #Update to next 
                idx = (idx+1)%len(key)
            #Otherwise
            else:

                #Increment
                cipher += i

        #Return
        return cipher
    #Define main
    def main():



        #Get text from user 
        text = input("Type a message:" )

        #Get key from user
        key = input("Encrption key:" )

        #Call method
        cipher = encrypt(text,key)

        #Display result
        print(cipher)

    #Call main
    if __name__ == "__main__":
        main()

✖︎对于vigenere.encrypt('BaRFoo','BaZ')

    You should have returned this:                              
        'Feqwgba <3 fnvta effo ox0xiv syfvbxf'                  

    But you actually returned this:                             
        'feqwgba <3 fnvta effo ox0xiv syfvbxf'                  

如何获取大写的首字母?

字母的位置和旋转字符如下:

    You should have returned this:                              
        'CaQGon'                                                

    But you actually returned this:                             
        'caqgon'   

0 个答案:

没有答案