问题描述
我正在尝试使用'python'构建非GUI应用程序,但由于某些原因,我的'main_menu()'函数没有返回我需要的变量
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()
解决方法
正如前面的答案所指出的,您不会在任何地方保存main_menu()
函数的返回值。但是您的代码中还存在其他一些错误,因此让我们首先解决这些错误。
- 您需要先定义函数,然后才能使用它。您似乎正在尝试调用
add_number
函数并同时定义它。首先定义您的函数,然后像这样调用它:
# Define the add_number() function
def add_number():
clean()
...
if main == 1:
# call the add_number() function
add_number()
- 您正在尝试迭代一个数字,这将引发错误。您可以尝试使用
range
函数代替。
number = int(input("----->"))
for i in range(number): # using range function
...
- 您正在尝试将名称转换为int,但是我想您可能希望它为字符串。
# 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-->")
- 您的try块正在捕获
SyntaxErrors
,但是您可能想捕获ValueErrors
。语法错误是代码语法中的错误,例如忘记了:
之类的内容。值错误是在某个日期的值错误时产生的错误,例如,当您尝试将字符串转换为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")
此循环将显示main_menu并询问用户一个选项。然后,如果用户选择1,它将运行add_number()
函数。完成该功能后,循环将重新开始并显示菜单。
看起来像这样:
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")
,
变量main
仅在main_menu
函数中具有作用域。您需要将main_menu()
的结果分配给某些东西才能使用它。
main = main_menu()
if main == 1:
...
,
调用函数时,需要将结果放入变量(或以其他方式使用)。
例如:
selection = main_menu()
函数内部定义的变量在函数完成后消失; return
语句仅返回值,而不是整个变量。
当您调用该函数时,它返回main,但是必须将其分配给某个对象,以便您对其进行操作。仅调用该函数将不会创建变量main。这是因为该功能的范围。
main = main_menu()
,
您的代码中有很多错误。 def =仅声明该函数,但未调用它。
首先,如前所述,调用main_menu()时需要执行以下操作:
main = main_menu()
第二,如果您应该调用adD_number函数:
if main ==1:
add_number()
但是请确保在调用前声明add_number()(使用def)。
祝你好运!