Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 对列表进行排序、删除和追加_Python 3.x_List_Sorting - Fatal编程技术网

Python 3.x 对列表进行排序、删除和追加

Python 3.x 对列表进行排序、删除和追加,python-3.x,list,sorting,Python 3.x,List,Sorting,我正在尝试创建一个程序,允许用户选择是否向列表中添加、删除或排序。但由于某种原因,它不起作用——它只是不排序/删除/添加。 以下是我所拥有的: countries = ["England", "Germany", "Spain", "Italy", "France", "Turkey", "Greece", "Austria", "America", "Ireland"] print(countries) print(" ") def menu (): countries = ["E

我正在尝试创建一个程序,允许用户选择是否向列表中添加、删除或排序。但由于某种原因,它不起作用——它只是不排序/删除/添加。 以下是我所拥有的:

countries = ["England", "Germany", "Spain", "Italy", "France", "Turkey", 
  "Greece", "Austria", "America", "Ireland"]
print(countries)
print(" ")

def menu ():
  countries = ["England", "Germany", "Spain", "Italy", "France", "Turkey", 
    "Greece", "Austria", "America", "Ireland"]

  print ("Select the option you require: \n \n 1. Add a new country \n 2. 
    delete a country \n 3. Sort the list \n ")

  option = input("Please enter 1, 2 or 3: ")

  if option == "1":
     addition = input("Enter the country you would like to add: ").title()
     countries = countries.append(addition)

  elif option == "2":
     removes = input("Enter the country you would like to delete").title()

     countries = countries.remove(removes)
  elif option == "3":
     countries.sort()

  else:
     print ("You can only enter 1, 2 or 3 \n")
     menu()

menu()
print(countries)
这是我选择对列表排序时控制台生成的结果:

 ['England', 'Germany', 'Spain', 'Italy', 'France', 'Turkey', 'Greece', 
 'Austria', 'America', 'Ireland']

Select the option you require: 

 1. Add a new country 
 2. Delete a country 
 3. Sort the list 

Please enter 1, 2 or 3:  3
['England', 'Germany', 'Spain', 'Italy', 'France', 'Turkey', 'Greece', 
 'Austria', 'America', 'Ireland']
当我希望列表按字母升序排序时。
任何帮助都将不胜感激,谢谢。

您必须两次定义所有国家,这一事实告诉您有些问题。您有两个不同的变量,都命名为国家,一个在模块范围内,一个在函数内部。一个廉价且简单的解决方案是使用全局关键字使模块级变量可用于函数内部的变异:


以这种方式使用全局变量通常被认为是不好的风格。请考虑将国家列表作为菜单函数的参数传递。< /P> < p>您正在对方法菜单中的国家列表进行排序,这对您的方法之外的列表没有影响。您可以在排序后打印列表

  elif option == "3":
      countries.sort()
      print(countries)
或者删除方法中的列表,您可以将列表交给该方法并将其返回

希望我能帮助您。

您的国家/地区内部菜单与外部菜单变量不同。。。所以,不管你在做什么——在调用菜单后,它不会对你正在打印的国家做任何事情
  elif option == "3":
      countries.sort()
      print(countries)