我是 python 新手,我有这个项目我正在做一个带有两个函数的小项目,其中第一个返回第一次在字符串中发现差异的索引。下一个函数执行此操作,但在字符串列表中。现在,由于我是业余爱好者,我使用了过多的 if 和 else 语句,这导致返回语句过多,尤其是在第二个函数中,并且出现错误 [R1710:不一致的返回语句]。我该如何修复它,谁能给我提供更好的代码片段的清晰示例?很抱歉问这么长的问题。
IDENTICAL = -1
def singleline_diff(line1, line2):
"""
Inputs:
line1 - first single line string
line2 - second single line string
Output:
Returns the index where the first difference between
line1 and line2 occurs.
Returns IDENTICAL if the two lines are the same.
"""
len1 = len(line1)
len2 = len(line2)
minimum_length = min(len1, len2)
if len1 != len2:
if minimum_length == 0:
return 0
for idx in range(minimum_length):
if line1[idx] == line2[idx]:
pass …Run Code Online (Sandbox Code Playgroud) 当我在int或字符串数组上运行Console.WriteLine()时,它将打印(System.String []),(System.Int32 [])。但是我用char数组执行此操作时,它显示的内容就好像它们是字符串一样。这种奇怪的行为是什么?(ihgfedcba)是它显示的内容。谁能解释为什么会这样?
public static void Test(){
string[] words_array = new string[9];
words_array[0] = "football";
words_array[1] = "handball";
words_array[2] = "Harry Potter";
words_array[3] = "Prometheus";
words_array[4] = "strengh";
words_array[5] = "Muscles";
words_array[6] = "weakness";
words_array[7] = "beauty";
words_array[8] = "Ali";
System.Console.WriteLine(words_array);
int[] int_array = new int[9];
int_array[0] = 0;
int_array[1] = 1;
int_array[2] = 2;
int_array[3] = 3;
int_array[4] = 4;
int_array[5] = 5;
int_array[6] = 6;
int_array[7] = 7;
int_array[8] = 8;
System.Console.WriteLine(int_array);
char[] char_array = new char[9];
char_array[0] …Run Code Online (Sandbox Code Playgroud)