Python 2.7 我怎样才能解决这个问题?带输入的Python列表

Python 2.7 我怎样才能解决这个问题?带输入的Python列表,python-2.7,list,input,Python 2.7,List,Input,我需要创建一个代码,如果您输入1,它将显示实际的列表,即空,然后如果您输入2,您将能够在列表中添加“x”,如果您按3,您可以在列表中删除,最后一件事是如果您按9,您将退出python 代码如下: list = [] if input == "1": print list if input == "2": list.append("hi") print list if input == "3": list.remove("hi") print list if

我需要创建一个代码,如果您输入1,它将显示实际的列表,即空,然后如果您输入2,您将能够在列表中添加“x”,如果您按3,您可以在列表中删除,最后一件事是如果您按9,您将退出python

代码如下:

list = []
if input == "1":
    print list
if input == "2":
    list.append("hi")
    print list
if input == "3":
    list.remove("hi")
    print list
if input == "9":
    sys.exit()
如果有人帮助我,我会很高兴的。

试试这个

import sys
list = []
ch = sys.stdin.read(1)
if ch == "1":
    print(list)
if ch == "2":
   list.append("hi")
   print(list)
if ch == "3":
   list.remove("hi")
   print(list)

if ch == "9":
   sys.exit()
试试这个

import sys
list = []
ch = sys.stdin.read(1)
if ch == "1":
    print(list)
if ch == "2":
   list.append("hi")
   print(list)
if ch == "3":
   list.remove("hi")
   print(list)

if ch == "9":
   sys.exit()
你可以试试这个。 我以为你每次都要输入,所以有一个无限的while循环{如果我错了,你只想输入一次,那么你可以删除while循环,它也会工作得很好}。 另外,我认为从列表中删除元素的方法是,您的意思是删除插入列表中的最新元素,因此此代码将删除插入的最新元素。 此外,如果存在除{1,2,3,9}以外的任何输入,则以下代码将处理它

希望这能解决你的问题

import sys
arr = []   #creates an empty list.

while True:

inp = raw_input("Enter the number you want to enter {1,2,3,9} :- ")

if inp == "1":
    print arr;

elif inp == "2":
    temp = raw_input("Enter what you want to add to the list :- ")
    arr.append(temp)
    print arr

elif inp == "3":
    arr = arr[:-1] ## will remove the last element , and will also handle even if there is no element present 
    print arr   

elif inp ==  "9":
    sys.exit()

else: #Any input other than {1,2,3,9}
    print "Please enter the input from {1,2,3,9}"   
你可以试试这个。 我以为你每次都要输入,所以有一个无限的while循环{如果我错了,你只想输入一次,那么你可以删除while循环,它也会工作得很好}。 另外,我认为从列表中删除元素的方法是,您的意思是删除插入列表中的最新元素,因此此代码将删除插入的最新元素。 此外,如果存在除{1,2,3,9}以外的任何输入,则以下代码将处理它

希望这能解决你的问题

import sys
arr = []   #creates an empty list.

while True:

inp = raw_input("Enter the number you want to enter {1,2,3,9} :- ")

if inp == "1":
    print arr;

elif inp == "2":
    temp = raw_input("Enter what you want to add to the list :- ")
    arr.append(temp)
    print arr

elif inp == "3":
    arr = arr[:-1] ## will remove the last element , and will also handle even if there is no element present 
    print arr   

elif inp ==  "9":
    sys.exit()

else: #Any input other than {1,2,3,9}
    print "Please enter the input from {1,2,3,9}"