Matthew Beckham Matthew Beckham - 1 year ago 80
Python Question

I need help a little help. (Is this out of my league right now?) (Python editing list using strings)

My script so far:

#DogReg v1.0
import time
Students = ['Mary', 'Matthew', 'Mark', 'Lianne' 'Spencer'
'John', 'Logan', 'Sam', 'Judy', 'Jc', 'Aj' ]

print("1. Add Student")
print("2. Delete Student")
print("3. Edit Student")
print("4. Show All Students")
useMenu = input("What shall you do? ")
if(useMenu != "1" and useMenu != "2" and useMenu != "3" and useMenu != "4"):
print("Invalid request please choose 1, 2, 3, or 4.")
elif(useMenu == "1"):
newName = input("What is the students name? ")
print(str(newName) + " added.")
elif(useMenu == "2"):
remStudent = input("What student would you like to remove? ")
print(str(remStudent) + " has been removed.")
elif(useMenu == "3"):

So I'm trying to be able to let the user input a name they want to edit and change it.

I tried looking up the function for editing list entries and I haven't found one I'm looking for I don't think unless I'm just not looking at certain things right.

Answer Source

How about this?

elif(useMenu == "3"):

    oldName = input("What student would you like to edit? ")

    if oldName in Students:

        index = Students.index(oldName)
        newName = input("What is the student's new name? ")
        Students[index] = newName
        print(str(oldName) + " has been edited to " + str(newName))


       print('Student' + oldName + 'not found!')


with output:

1. Add Student
2. Delete Student
3. Edit Student
4. Show All Students
What shall you do? '3'
What student would you like to edit? 'Mary'
What is the student's new name? 'Marry'
Mary has been edited to Marry
['Marry', 'Matthew', 'Mark', 'LianneSpencerJohn', 'Logan', 'Sam', 'Judy', 'Jc', 'Aj']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download