python - 为什么我的 Python 函数没有返回任何值?
问题描述
我正在尝试使用“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
。语法错误是代码语法中的错误,例如忘记 a:
或其他内容。值错误是在某些日期的值错误时产生的错误,例如当您尝试将字符串转换为 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")
推荐阅读
- c# - 如何为在其主体中调用另一个方法的方法编写 xUnit Test?
- node.js - AWS JavaScript s3Client 图像上传中断
- android - Where is the .exe in flutter?
- c++ - 具有正确限制的无限 for 循环
- python - 如何获取发票付款金额 - 自定义报告
- javascript - MediaStream Recording API 对画布元素的视频捕获不起作用
- javascript - 尝试访问 /about 路由时出现“无法获取 /”错误
- javascript - Discord.js 删除角色
- c# - EPPlus:另存为特定用户
- sql-server - 审核日志记录未显示在 CRM 视图中