【问题标题】:Want to receive argument from user and put them on function想要从用户那里接收参数并将它们放在函数中
【发布时间】:2022-01-06 00:31:35
【问题描述】:

我想从用户那里获取参数并在函数中使用它们,但是类型不同。

注意:代码底部有一条正确运行的注释 但我想直接获取参数

感谢您的帮助

from string import ascii_letters

def encrypt(string , key):
    alpha = ascii_letters
    result = ''

    for ch in string : 
        if ch not in alpha :
            result += ch
        else :
            new_key = (alpha.index(ch) + key.str() ) % len(alpha)
            result += alpha[new_key]
    return result

def decrypt(string , key):
    key *= -1
    return encrypt(string , key)
state = ''
print("please enter 'e' to encrpyt & type 'd' to decrpyt :...")
state = input()

if state == 'e' : 
    print("please enter the str that you want to encrypt :... ")
    c_str1 = input()
    print("\nplease enter the key that shifts strings(e.x. 1 to 52):... ")
    c_in1=input()
    encrypt(c_str1, c_in1)

elif state == 'd':
    print("please enter the str that you want to decrypt :... ")
    c_str= input('')
    print("\nplease enter the key (e.x. 1 to 52):... ")
    c_in = input()
    decrypt(c_str, c_in )
   
# print(decrypt('amir',4))

【问题讨论】:

    标签: encryption user-input command-line-arguments public-key-encryption caesar-cipher


    【解决方案1】:

    不确定您的问题是什么,我尝试让您的代码运行, 不确定是否按预期运行:

    from string import ascii_letters
    
    def encrypt(string , key):
        result = ''
        alpha = ascii_letters
        for ch in string : 
            if ch not in alpha :
                result += ch
            else :
                new_key = (alpha.index(ch) + int(key))  % len(alpha)
                
                result += alpha[new_key]
        return result
    
    def decrypt(string , key):
        key *= -1
        return encrypt(string , key)
    
    
    state = ''
    print("please enter 'e' to encrpyt & type 'd' to decrpyt :...")
    state = input()
    
    if state == 'e' : 
        print("please enter the str that you want to encrypt :... ")
        c_str1 = input()
        print("\nplease enter the key that shifts strings(e.x. 1 to 52):... ")
        c_in1 = input()
        print(encrypt(c_str1, c_in1))
    
    elif state == 'd':
        print("please enter the str that you want to decrypt :... ")
        c_str = input()
        print("\nplease enter the key (e.x. 1 to 52):... ")
        c_in = int(input())
        print(decrypt(c_str, c_in ))
    

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2015-11-21
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      相关资源
      最近更新 更多