Matthew Beckham Matthew Beckham - 2 months ago 7
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? ")
Students.append(newName)
time.sleep(1)
print(str(newName) + " added.")
time.sleep(1)
print(Students)
elif(useMenu == "2"):
remStudent = input("What student would you like to remove? ")
Students.remove(remStudent)
time.sleep(1)
print(str(remStudent) + " has been removed.")
time.sleep(1)
print(Students)
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

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
        time.sleep(1)
        print(str(oldName) + " has been edited to " + str(newName))
        time.sleep(1)

    else: 

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

    print(Students)

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']