我目前正在接受我的第一个python类,我需要帮助分配数字到电子邮件时,它被添加到我的电子邮件列表。这样,当有人想从列表中删除电子邮件时,他们可以看到该电子邮件分配给了什么号码,只需输入号码即可。我将提供我的代码,以及最终产品应该是什么样子的视频。
https://www.youtube.com/watch?v=mWTpdMaxTvI& ab_channel = JenniferBailey
注:我不知道所有的专业术语,所以我希望你们能理解。谢谢!
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")
没有尝试过很多,不知道从哪里开始。
Salvatore!不错的代码,我想你已经很接近解决方案了。在Python中,有一个名为enumerate的内置函数,它自动分配一个数字以在列表中拥有一个计数器。
在你的代码中,我会添加这样的东西:
*注意,当变量menu = 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")
希望能帮到你!!