首页 > 解决方案 > 为什么我的 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()

标签: pythonpandasfunction

解决方案


正如前面的答案所指出的,您没有将main_menu()函数的返回值保存在任何地方。但是您的代码中还有一些其他错误,所以让我们先解决这些错误。

  1. 您需要先定义您的功能,然后才能使用它。您似乎正在尝试调用该add_number函数并同时定义它。首先定义你的函数,然后像这样调用它:
# Define the add_number() function
def add_number():
    clean()
    ...

if main == 1:
    # call the add_number() function
    add_number()
    
  1. 您正在尝试迭代一个数字,这将引发错误。您可以尝试使用此range功能。
number = int(input("----->"))
for i in range(number): # using range function
   ...
  1. 您正在尝试将名称转换为 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-->")
  1. 您的 try 块正在捕获SyntaxErrors,但您可能想要捕获ValueErrors。语法错误是代码语法中的错误,例如忘记 a:或其他内容。值错误是在某些日期的值错误时产生的错误,例如当您尝试将字符串转换为 int 时。
# replace SyntaxError with ValueError
except ValueError:
    print("Oops something went wrong!")
  1. 最后,如果您想在输入联系电话后返回菜单,您将需要某种循环。
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")

推荐阅读