Message=if you give only one argument to maketrans it must be a dict Source=E:\1daer\mmxdzy\1\dzy1\dzy1.py StackTrace: File "E:\1daer\mmxdzy\1\dzy1\dzy1.py", line 33, in decrypt mapping = str.maketrans(key) ^^^^^^^^^^^^^^^^^^ File "E:\1daer\mmxdzy\1\dzy1\dzy1.py", line 47, in <module> (Current frame) print(decrypt(cipher_text, key)) ^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: if you give only one argument to maketrans it must be a dict
时间: 2023-08-02 12:09:09 浏览: 84
这个错误提示是在使用 `str.maketrans()` 函数时发生的。按照提示,如果您只给 `maketrans()` 函数传递一个参数,则它必须是一个字典对象,用于指定要替换的字符映射表。
在您的代码中,`maketrans()` 函数的参数 `key` 只是一个字符串。因此,Python 将其解释为要替换的字符序列,而不是映射表。这样会导致 `maketrans()` 函数抛出上述错误。
要解决此问题,请将 `key` 参数转换为映射表字典。您可以使用以下代码转换:
```python
mapping = str.maketrans({key[i]: key[(i+1)%len(key)] for i in range(len(key))})
```
这将使用字典生成式创建一个映射表字典,然后将其传递给 `maketrans()` 函数。这样,您就可以通过传递一个字典对象来调用 `maketrans()` 函数,而不是一个字符串对象。
相关问题
Argument 1: cannot convert from 'string' to 'int'
This error message usually occurs when you try to assign a string value to an integer variable or pass a string argument to a method that expects an integer argument. To resolve this issue, you need to convert the string value to an integer using the appropriate method.
For example, if you have a string variable named "myString" that contains an integer value, you can convert it to an integer using the Convert.ToInt32() method:
```
string myString = "123";
int myInt = Convert.ToInt32(myString);
```
Alternatively, you can use the int.TryParse() method to convert the string value to an integer safely:
```
string myString = "123";
int myInt;
if (int.TryParse(myString, out myInt))
{
// Conversion successful
}
else
{
// Conversion failed
}
```
In both cases, you will end up with an integer variable that you can use in your code without getting the "cannot convert from 'string' to 'int'" error message.
阅读全文