Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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中创建简单菜单?_Python_Python 3.x_List - Fatal编程技术网

如何使用列表函数在Python中创建简单菜单?

如何使用列表函数在Python中创建简单菜单?,python,python-3.x,list,Python,Python 3.x,List,请看一下我被要求做的练习。我现在正努力工作几个小时来让它工作 必须有一个包含以下选项的菜单:添加数字、删除数字(输入占位符)、显示列表。 每次做出选择时,程序都应该询问是否要重新运行脚本 我尝试过循环和函数,但它似乎不适合我 请参阅下面的代码 提前谢谢你 def list(): operation = input(''' Select operation: [1] Add number to the list [2] Remove number from the list [3] Di

请看一下我被要求做的练习。我现在正努力工作几个小时来让它工作

必须有一个包含以下选项的菜单:添加数字、删除数字(输入占位符)、显示列表。 每次做出选择时,程序都应该询问是否要重新运行脚本

我尝试过循环和函数,但它似乎不适合我

请参阅下面的代码

提前谢谢你

def list():

    operation = input('''
Select operation:
[1] Add number to the list
[2] Remove number from the list
[3] Display list

''')
    mylist = []

    if operation == '1':
        print("Type the number you would like to add to the list: ")
        number = int(input())
        mylist.append(number)

    elif operation == '2':
        print("Type position of the element number you like to remove from the list: ")
        number = int(input())
        mylist.pop(number)

    elif operation == '3':
        print(mylist)

    else:
        print('You have not chosen a valid operator, please run the program again.')

    again()

def again():
    list_again = input('''
Would you like to see main menu again? (Y/N)
''')

    if list_again.upper() == 'Y':
        list()
    elif list_again.upper() == 'N':
        print('OK. Bye bye. :)')
    else:
        again()

list()

尝试将
mylist=[]
移动到函数外部

尝试将
mylist=[]
移动到函数外部

可以用于多种菜单。

可以用于多种菜单。

验证代码

mylist = []
def list():

    operation = input('''Select operation:\n [1] Add number to the list \n [2] Remove number from the list \n [3] Display list\n ''')


    if operation == '1':
        print("Type the number you would like to add to the list: ")
        number = int(input())
        mylist.append(number)

    elif operation == '2':
        print("Type position of the element number you like to remove from the list: ")
        number = int(input())
        mylist.pop(number)

    elif operation == '3':
        print(mylist)

    else:
        print('You have not chosen a valid operator, please run the program again.')

    again()

def again():
    list_again = input('''Would you like to see main menu again? (Y/N)''')

    if list_again.upper() == 'Y':
        list()
    elif list_again.upper() == 'N':
        print('OK. Bye bye. :)')
    else:
        again()

list()

干杯

验证代码

mylist = []
def list():

    operation = input('''Select operation:\n [1] Add number to the list \n [2] Remove number from the list \n [3] Display list\n ''')


    if operation == '1':
        print("Type the number you would like to add to the list: ")
        number = int(input())
        mylist.append(number)

    elif operation == '2':
        print("Type position of the element number you like to remove from the list: ")
        number = int(input())
        mylist.pop(number)

    elif operation == '3':
        print(mylist)

    else:
        print('You have not chosen a valid operator, please run the program again.')

    again()

def again():
    list_again = input('''Would you like to see main menu again? (Y/N)''')

    if list_again.upper() == 'Y':
        list()
    elif list_again.upper() == 'N':
        print('OK. Bye bye. :)')
    else:
        again()

list()

干杯

正如其他人所指出的,需要移动
mylist
变量。 也就是说,我发现返回输入查询的机制可以改进。请参阅下面的代码。在这里,您可以将所有内容都保存在一个函数中,而不必反复询问用户是否希望继续,它将不确定地继续,直到您有意识地
中断

def main():
    mylist = []
    while True:
        operation = input('''
Select operation:
[1] Add number to the list
[2] Remove number from the list
[3] Display list
[4] Exit programm

''')
        if operation == '1':
            print("Type the number you would like to add to the list: ")
            number = int(input())
            mylist.append(number)

        elif operation == '2':
            print("Type position of the element number you like to remove from the list: ")
            number = int(input())
            mylist.pop(number)

        elif operation == '3':
            print(mylist)

        elif operation == '4':
            break

        else:
            print("Invalid choice. Please try again.")

main()

边栏:“list”是python中的一个固定表达式。我会避免使用它和其他人喜欢的变量名。

正如其他人所指出的,您的
mylist
变量需要移动。 也就是说,我发现返回输入查询的机制可以改进。请参阅下面的代码。在这里,您可以将所有内容都保存在一个函数中,而不必反复询问用户是否希望继续,它将不确定地继续,直到您有意识地
中断

def main():
    mylist = []
    while True:
        operation = input('''
Select operation:
[1] Add number to the list
[2] Remove number from the list
[3] Display list
[4] Exit programm

''')
        if operation == '1':
            print("Type the number you would like to add to the list: ")
            number = int(input())
            mylist.append(number)

        elif operation == '2':
            print("Type position of the element number you like to remove from the list: ")
            number = int(input())
            mylist.pop(number)

        elif operation == '3':
            print(mylist)

        elif operation == '4':
            break

        else:
            print("Invalid choice. Please try again.")

main()

边栏:“list”是python中的一个固定表达式。我会避免使用它和其他喜欢它的变量名。

我强烈反对使用
list
作为函数名,因为它在Python中定义为内置类型。关于
input\u number\u list
,我强烈反对使用
list
作为函数名,因为它在Python中定义为内置类型。关于<代码>输入-数字-列表< /代码>?考虑缩进属于 List](/CODE)的代码,这将不会按IS运行。@ JojuasasChLITCHIT考虑缩进。考虑缩进属于<代码>表()/<代码>的代码,这将不会按原样运行。@ JojHuasasChLITCHIT考虑缩进。