As the previous answers have pointed out, you are not saving the return value of the main_menu() function anywhere. But you also have a few other errors in your code, so let's address those first.
- You need to define your function before you can use it. You seem to be trying to call the
add_number function and define it at the same time. First define your function, and then call it something like so:
# Define the add_number() function
def add_number():
clean()
...
if main == 1:
# call the add_number() function
add_number()
- You are trying to iterate a number, which will throw an error. You can try and use the
range function for this instead.
number = int(input("----->"))
for i in range(number): # using range function
...
- You are trying to convert the name to an int, but I assume you probably want it to be a string.
# this will throw an ValueError if you type a name like "John"
c_n = int(input("Name-->"))
# This will not throw an error because you are not converting a string into an int
c_n = input("Name-->")
- Your try block is catching
SyntaxErrors, but your probably want to catch ValueErrors. A syntax error is an error in your code syntax, like forgetting a : or something. While a value error is an error that is produced when some date is of the wrong value, like when you trying and convert a string to an int.
# replace SyntaxError with ValueError
except ValueError:
print("Oops something went wrong!")
- Finally if you want to go back to the menu after you enter your contact numbers, you will need some kind of loop.
while(True):
# here we are saving the return value main_menu() function
choice = main_menu()
if choice == 1:
add_number()
# add other options here
else:
print("Sorry that option is not available")
This loop will show the main_menu and ask the user for an option. Then if the user chooses 1 it will run the add_number() function. Once that function is done, the loop will start over and show the menu.
All together that looks like this:
import pandas as pd
#list for containing telephone number
telephone = []
#list containing contact name
contact_name = []
def main_menu():
intro = """ ----------------------WElCOME to MyPhone-----------------------
To select the task please type the number corrosponding to it
1)Add New Number
2)Remove Contact
3)Access old contact
----> """
main = int(input(intro))
return main
def clean():
print("--------------------------------------------------------------------------------------")
def add_number():
clean()
try:
print("How many number(s) you want to add. Remember if you don't want to add any number just click enter",end="")
number = int(input("----->"))
for i in range(number):
c_n = input("Name-->")
t_n = int(input("Number-->"))
contact_name.append(c_n)
telephone.append(t_n)
else:
print("Contacts are Saved!👍")
except ValueError:
print("Oops something went wrong!")
while(True):
choice = main_menu()
if choice == 1:
add_number()
# add other options here
# catch any other options input
else:
print("Sorry that option is not available")
print(main_menu()), orval = main_menu().