为什么我们得不到正确的输出? [英] Why dont we get the right output?
问题描述
我们正在以四人一组的方式工作,并且花了最后几个小时来调试/修复我们的代码以获得正确的输出。您将在下面找到我们的代码,其中包含字符串所在的列表和所需的输出。请帮忙。
我们的代码是:
We are working in a group of four and have spent the last few hours tring to debug/fix our code to get the right output. Below you will find our code the list in which the string is in and required output. Please help.
Our code is:
def reverse(my_list, number):
string = ""
new_list = []
count = 0
if number > len(my_list):
number = len(my_list)
if number < 2:
for k in my_list:
new_list.append(k)
return my_list
for index in range(number-1,-1,-1):
new_list.append(my_list[index])
for index in range (number, len(my_list)):
new_list.append(my_list[index])
return new_list
list其中包含字符串:
list in which string is in:
print("\nreverse Test")
str_list6 = ['e', 'd', 'u', 'd']
str_list7 = ['m', 'o', 'b', 'b', 'e', 'd']
new_list = list_function.reverse(str_list6, 4)
print(new_list)
new_list = list_function.reverse(str_list7, 3)
print(new_list)
new_list = list_function.reverse(str_list6)
print(new_list)
必填输出是:
Required output is:
reverse Test
['d', 'u', 'd', 'e']
['b', 'o', 'm', 'b', 'e', 'd']
['d', 'u', 'd', 'e']
我尝试过:
我们正在以四人一组的形式工作花了最后几个小时来调试/修复我们的代码以获得正确的输出。您将在下面找到我们的代码,其中包含字符串所在的列表和所需的输出。请帮助。
What I have tried:
We are working in a group of four and have spent the last few hours tring to debug/fix our code to get the right output. Below you will find our code the list in which the string is in and required output. Please help.
推荐答案
试试这个:
Try this:
def reverse(my_list, number = 0):
newlist = []
if number == 0:
number = len(my_list)
for x in range(number-1, -1, -1):
newlist.append(my_list[x])
for x in range(number, len(my_list)):
newlist.append(my_list[x])
return newlist
这篇关于为什么我们得不到正确的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!