我正在尝试将下面的C代码转换为Python。我没有C方面的经验,但有一点Python方面的经验。你知道吗
main( int argc, char *argv[])
{
char a[] = "ds dsf ds sd dsfas";
unsigned char c;
int d, j;
for(d = 0; d < 26; d++)
{
printf("d = %d: ", d);
for (j = 0; j < 21; j++ )
{
if( a[j] == ' ')
c = ' ';
else
{
c = a[j] + d;
if (c > 'z')
c = c - 26;
}
printf("%c", c);
}
printf("\n");
}
我已经设法做到这一点:我在哪里得到一个列表索引超出范围的异常,有什么建议吗?你知道吗
d=0
a=["ds dsf ds sd dsfas"]
while (d <26):
print("d = ",d)
d=d+1
j=0
while(j<21):
if a[j]=='':
c =''
else:
c = answer[j]+str(d)
if c>'z':
c=c-26
j=j+1
print("%c",c)
我希望这能实现您的C代码想要实现的目标:
见此,已解释为:
循环一直执行到j变为21。但是我认为在
a
列表中没有那么多元素。这就是为什么你会出错。我想len(a)
是18。因此,将循环更改为:或者
将清除错误
相关问题 更多 >
编程相关推荐