New to python. I need help assigning numerical values to an email list

Question:

I am currently taking my first python class and I need help assigning numbers to an email when it is added to my list of emails. That way when someone wants to remove an email from the list, they can see what number the email is assigned to and just type in the number. I will provide my code, and a video of what the final product should look like.

https://www.youtube.com/watch?v=mWTpdMaxTvI&ab_channel=JenniferBailey

P.S. I don’t know all the technical terms so I hope that made sense. Thanks!

menu = 0
list = []
while menu < 4:
    print(" 1 to add an emailn 2 to remove an emailn 3 to print listn 4 to EXIT menu")
    print("")
    menu = int(input("Enter: "))
    print("")
    if menu == 1:
        enter_email = input("What is the email: ")
        list.insert(1, enter_email)
        print("")
        print("The email:", enter_email, "was successfully added to the list")
        print("")
    elif menu == 2:
        remove_email = input("What email would you like to remove: ")
        if remove_email in list:
            list.remove(remove_email)
            print("")
            print("The email, ", remove_email, ", was removed from the list")
            print("")
        else:
            print("")
            print("That email does not exist")
            print("")
    elif menu == 3:
            email_list = ', '.join(str(item) for item in list)
            print("")
            print(email_list)
            print("")
    elif menu == 4:
        print("Exited successfully, have a nice day")

Have not tried much and don’t really know where to start.

Asked By: Salvatore

||

Answers:

Salvatore!
Nice code, I think you are pretty close to the solution. In Python, there is a built-in function called enumerate that assign automatically a number to have a counter within a list a link.

In your code, I would add something like this:

*Note I just changed the code when the variable menu equals 3

menu = 0
list = []
while menu < 4:
    print(" 1 to add an emailn 2 to remove an emailn 3 to print listn 4 to EXIT menu")
    print("")
    menu = int(input("Enter: "))
    print("")
    if menu == 1:
        enter_email = input("What is the email: ")
        list.insert(1, enter_email)
        print("")
        print("The email:", enter_email, "was successfully added to the list")
        print("")
    elif menu == 2:
        remove_email = input("What email would you like to remove: ")
        if remove_email in list:
            list.remove(remove_email)
            print("")
            print("The email, ", remove_email, ", was removed from the list")
            print("")
        else:
            print("")
            print("That email does not exist")
            print("")
    elif menu == 3:
      for counter, value in enumerate(list):
        print(counter, value)
        print("")
    elif menu == 4:
        print("Exited successfully, have a nice day")

Hope it can help you!!!

Answered By: Gof.io
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.