Reputation: 15
I am trying to build a non GUI application using 'python' but for some reason my 'main_menu()' function is not returning me variable i need
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
main_menu()
def clean():
print("--------------------------------------------------------------------------------------")
if main ==1:
def add_number():
clean()
try:
print("How many number(s) you want to add. Remeber if you don't want to add any number just click enter",end="")
number = int(input("----->"))
for i in number:
c_n = int(input("Name -->"))
t_n = int(input("Number-->"))
contact_name.append(c_n)
telephone.append(t_n)
else:
print("Contacts are Saved!👍")
except SyntaxError:
main_menu()
Upvotes: 0
Views: 132
Reputation: 101
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.
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()
range
function for this instead.number = int(input("----->"))
for i in range(number): # using range function
...
# 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-->")
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!")
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")
Upvotes: 2
Reputation: 6564
You have alot of errors in your code. def = only declares the function but is not calling it.
First as previously answers you need when calling the main_menu() to do:
main = main_menu()
Second, in the if you should call the adD_number function:
if main ==1:
add_number()
But make sure you declare the add_number() (with def) before calling it.
Good luck!
Upvotes: 0
Reputation: 403
When you call the function, it returns main, however, it must be assigned to something for you to do something with it. Just calling the function will not create the variable main. This is because of the scope of the function.
main = main_menu()
Upvotes: 0
Reputation: 6930
When you call the function, you need to put the result in a variable (or use it in some other way).
For instance:
selection = main_menu()
The variables defined inside a function go away after the function is finished; the return
statement returns just the value, not the whole variable.
Upvotes: 0
Reputation: 5449
The variable main
only has scope within the main_menu
function. You need to assign the result of main_menu()
to something to be able to use it.
main = main_menu()
if main == 1:
...
Upvotes: 1