Based on this ideea

You just wrote a loop allowing the input of 20 names into a list. Alas, you made an error as you entered name number 17. Redesign your input loop, so you can correct the error easily without having to retype the previous 16 names.

I wrote the following program

#!/usr/bin/python/ env

def input_numbers():

    #instantiate each variable
    elements = []
    i=0
    # loop from 0 to 5 or iterate
    for i in range(0,5):

        numbers = raw_input("Enter a number:") 

    # add each number to the list
        elements.append(numbers)

    # option chooser
    op = raw_input("Do you want to change an element in the list? y/n:")
    # as long as op is true do this block of code
    while op =="y":

        #option for exit(not a good one)
        op = raw_input("Are you going to change any elements after this one? y/n:")


        d = int(raw_input("Enter the position of the element:"))

        n = int(raw_input("Enter the new value:"))

        #change an element in the list using position and new value
        elements[d]=n



    return elements


print "Your list is "+str(input_numbers())

I would like to know if there is a better way of creating this program,a much more performance driven way?

Thank you for any input

Recommended Answers

All 2 Replies

def lst_corrector(lst, index_vaule):
    value_change = raw_input('Enter correct value: ')
    for index, item in enumerate(lst):
        if index == index_to_change:
            lst[index] = value_change
            return lst

#Error should be car
lst = ['boat', 'train', 'carfd']
index_to_change = 2
print lst_corrector(lst, index_to_change)
#--> ['boat', 'train', 'car']
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.