简体   繁体   English

在Python中的两个函数之间传递字符串变量

[英]Passing a string variable between two functions in Python

I am having trouble passing a variable from one function to another: 我在将变量从一个函数传递到另一个函数时遇到麻烦:

def name():

    first_name=input("What is your name? ")

    if len(first_name)==0:
            print("\nYou did not enter a name!")
            return name()

    else:
            print("\nHello %s." % first_name)
            return surname()


def surname():

    last_name=input("\nCan you tell my your last name please?")

    if len(last_name)==0:
            print("\nYou did not enter a last name!")
            return surname()
    else:

            print("Nice to meet you %s %s." % (first_name,last_name))

I want the last command to print the inputed first_name from def name() and last name from def surname() 我希望最后一个命令从def name()打印输入的first_name和从def surname()打印def surname()

I always get the error that first_name is not defined and I do not know how to import it from the first function. 我总是收到未定义first_name的错误,并且我不知道如何从第一个函数导入它。 The error I get is: 我得到的错误是:

    print("Nice to meet you %s %s." % (first_name,last_name))
NameError: name 'first_name' is not defined

What am I doing wrong? 我究竟做错了什么?

You need to pass the information in the function call: 您需要在函数调用中传递信息:

def name():

    first_name = input("What is your name? ")

    if len(first_name) == 0:
        print("\nYou did not enter a name!")
        return name()

    else:
        print("\nHello %s." % first_name)
        surname(first_name)  # pass first_name on to the surname function


def surname(first_name): #first_name arrives here ready to be used in this function

    last_name = input("\nCan you tell my your last name please?")

    if len(last_name) == 0:
        print("\nYou did not enter a last name!")
        surname(first_name)
    else:
        print("Nice to meet you %s %s." % (first_name,last_name))

name()
def functionname(untypedparametername):
    # do smth with untypedparametername which holds "Jim" for this example   

name = "Jim"

functionname(name) # this will provide "Jim" to the function

You can see how they are used if you look at the examples in the documentation, fe here: https://docs.python.org/3/library/functions.html 如果您在以下文档中查看示例,则可以看到它们的用法: https : //docs.python.org/3/library/functions.html

Maybe you should read up on some of the tutorials for basics, you can find lots of them on the python main page: https://wiki.python.org/moin/BeginnersGuide 也许您应该阅读一些基本的教程,您可以在python主页上找到很多: https : //wiki.python.org/moin/BeginnersGuide

You can also use while loop to ask the names constantly until there is a valid input. 您也可以使用while循环不断询问名称,直到输入有效为止。

def name_find():
   while True:
       first_name=raw_input("What is your name? ")
       if len(first_name)==0:
           print("\nYou did not enter a name!")
           return name_find()
       else:
           print("\nHello %s." % first_name)
           return surname(first_name)
def surname(first_name):
   while True:
      last_name=raw_input("\nCan you tell me your last name please?")
      if len(last_name)==0:
          print("\nYou did not enter a last name!")
     else:
          print "Nice to meet you %s %s." % (first_name, last_name)
          break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM