we are going to create a multi-dimensional list to be able to store telephone numbers and user names together.
First we are going to create a function to run our menu within a loop:
def list():
userlist=[["Jimmy",+34666777888],["Bobby",+34668669449],["Charlie",+341999101110]]
while True:
print ("*"*43)
print ("\n* Multi-Dimensional List *\n")
print ("*"*43)
print ("\n* 1 - Display the users information *\n")
print ("* *\n")
print ("* 2 - Add a new user *\n")
print ("* *\n")
print ("* 3 - Count the number of users *\n")
print ("* *\n")
print ("* 4 - Exit *\n")
print ("*"*43)
As you can see we added some users already for testing purposes.
After setting the menu we are going to code the conditional statements for the different values of our user input:
action = int(input("Please type the number to perform the action selected:\n"))
To display the elements without brackets:
if action == 1:
for i in range(len(userlist)) :
for j in range(len(userlist[i])) :
print(userlist[i][j], end=" ")
print()
continue
Adding new users:
elif action == 2:
print("Type new user name: ")
newusername=input()
print("Type new telephone number")
newusertelephone=input()
newuser="\n".join([newusername,newusertelephone])
userlist.append([newuser])
For the next step we use len so we are going to count only the elements of the list that contains strings:
elif action == 3:
usercounter=len(userlist)
print("Numbers of users:",usercounter)
To exit the program we break the loop:
elif action == 4:
break
else:
print("Invalid choice. Please try again.")
list()
Top comments (0)