我想从用户那里获取参数并在函数中使用它们,但类型不同。
注意:正确工作的代码底部有一个注释但我想直接得到论点
感谢您的帮助
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))
不确定你的问题是什么,这里我尝试让你的代码运行,不确定是否按预期运行:
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 ))