我的教授指定的代码存在一些问题。我已经阅读了几本教科书,查看了不同的资源,但仍然没有多少运气。这是我教授的要求。
这是我的代码:
def main():
states1 = ['AL', 'AK', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA']
for words in states1:
print(words,\
end = " ")
states2 = states1[3:7]
list_func(states2)
def list_func(states2):
states2.pop(1)
states2.insert(1, 'TX')
state_variable = print(input("Enter the abbreviation of another state of your choice: "))
states2.append(state_variable)
states2 = states2[::-1]
returned_states = states2
print(returned_states)
return returned_states
main()
每当我运行程序时,我最终只得到代码的第一部分 - 因为在状态中打印出来的很好而且整洁。从那里,我不知道要继续。我希望得到一些正确方向的帮助。谢谢。
代码编辑1:
def main():
states1 = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA"]
for words in states1:
print(words, end=" ")
states2 = states1[3:7]
def list_func(states2):
states2.pop(1)
states2.insert(1, 'TX')
state_variable = input("Enter the abbreviation of another state of your choice: ")
states2.append(state_variable)
states2.reverse(states2)
returned_states = list_func(states2)
print(returned_states)
return states2
def main():
list_func()
main()
代码编辑2:
def list_func(states2):
states1 = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA"]
for words in states1:
print(words, end=" ")
states2 = states1[3:7]
states2.pop(1)
states2.insert(1, 'TX')
state_variable = input("Enter the abbreviation of another state of your choice: ")
states2.append(state_variable)
states2.reverse(states2)
returned_states = list_func(states2)
print(returned_states)
return states2
list_func(states2)
答案 0 :(得分:0)
让我告诉你如何解决这个问题。
您的main()
功能(几乎)很好。您只需要做一件事:您必须使用list_func
变量拨打states2
。
所以:
def list_func(states2):
states2.pop(1)
states2.insert(1, 'TX')
state_variable = input("Enter the abbreviation of another state of your choice: ")
states2.append(state_variable)
states2 = states2[::-1] # REVERSE A LIST LIKE THIS
returned_states = states2
print(returned_states)
return returned_states
def main():
states1 = ["AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA"]
for words in states1:
print(words, end=" ")
states2 = states1[3:7]
list_func(states2) # call list_func with your states2 variable.
main()
这应该有效。您的list_func
功能已损坏。这应该解决它。
答案 1 :(得分:0)
因此,您需要查看一些主要概念,即函数如何工作以及如何将参数传递给函数。幸运的是,在Python中有很多在线资源。
现在,我将解释为什么你的代码不起作用。你已经添加了第二个主要功能(并做了一些奇怪的缩进)。你不需要那个新的main函数,因为你已经有了一个正常工作的main函数。
这是您原来的main
功能:
def main():
states1 = ["AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA"]
for words in states1:
print(words, end=" ")
states2 = states1[3:7]
让我们完成你的任务步骤:
按字母顺序创建一个名为states1的列表,其中包含前10个美国州的双字母缩写。 (CHECK)
使用循环处理列表并在同一行显示所有缩写,用单个空格分隔。请参阅示例输出。 (CHECK)
通过切片创建名为states2的第二个列表。这个新列表必须包含states1中的中间四个州缩写。 (CHECK)
执行名为list_func的函数,其中states2为唯一参数
好的,这就是问题所在。打印状态后,您需要在list_func
中依次执行main
。您可以使用以下方法执行此操作:
states2 = list_func(states2)
这是因为list_func
接受了一个参数(states2
)所以当你调用它时,你需要传递一个参数。
这些是我在你的任务中克服这个困难的建议,将来会遇到问题,但是先尝试自己解决,然后如果你绝对需要,请来StackOverflow,询问有关你特定部分的具体问题代码。
这是我的代码建议:
def main():
states1 = ["AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA"]
for words in states1:
print(words, end=" ")
states2 = states1[3:7]
states2 = list_func(states2)
# don't forget to print here
def list_func(states2):
states2.pop(1)
states2.insert(1, 'TX')
state_variable = input("Enter the abbreviation of another state of your choice: ")
states2.append(state_variable)
states2.reverse(states2)
returned_states = list_func(states2)
print(returned_states)
return states2
main()