Concept of List ( User-Input)

•

In this post we will learn how to take user-input and add or remove the items from the list…
I’ll show you the code that how it works but you will download that code and modify it according to as you want

Let’s start with the code

# declared the list
data = ['planets', 'cars' , 'admin'] 

after declaring the list we will use input function from python that function will recieve input from the keyboard

# type add / remove as per the user wants
Input = input('Would you like to add/remove the item: ')    

after using the input method we will use now the if else statements for triggered the specific conditions decided by user..

if Input == 'add': # if user input add execute this 
    Input = input('Add New Item: ')
    data.append(Input) # this Input is variable from the if statement 
    print('Your list is updated...')
    print(data)
if Input == 'remove':
    Input = input("Enter item want's to remove: ")
    data.remove(Input)
    print("Your list is updated")
    print(data)
else:
    print('\n')
    print('Thanks for using the code....')

NOTE:- In the above first if statement is executed if the user chooses add and second will execute if the user choose remove..

Leave a Reply

Your email address will not be published. Required fields are marked *