如何从func1中获取var1并在func2中使用它,而又不会在func1中运行两次

时间:2019-04-21 18:02:47

标签: python python-3.x

我有func1,其中包含一个变量,并且我想在func2中访问该变量。我在下面的代码中尝试过返回func1中的变量,然后将变量"user_name"设置为函数"first_name_information",但这会使func1运行两次,我不这样做不想发生。

def func1():
    user_name = input("What's your name? ")

    if any(char.isdigit() for char in user_name):
        print("You can't put a number in your name.")
        sys.exit()
    else:
        pass

    return user_name

def func2():
    user_name = first_name_information()
    last_name = input("What's your last name {}? ".format(user_name))

    if any(char.isdigit() for char in last_name):
        print("You can't put a number in your last name.")
        sys.exit()
    else:
        pass

3 个答案:

答案 0 :(得分:0)

您可以通过几种方法执行此操作,但是这可能对您的情况有好处,您只需从func2调用func1:

def func1():
    user_name = input("What's your name? ")
    if any(char.isdigit() for char in user_name):
        print("You can't put a number in your name.")
        sys.exit()
    return user_name

def func2():
    user_name = func1()
    last_name = input("What's your last name {}? ".format(user_name))

    if any(char.isdigit() for char in last_name):
        print("You can't put a number in your last name.")
        sys.exit()
    return (user_name, last_name)

func2()

您将需要从第二个函数返回并携带两个值(可能以元组的形式)。

似乎您正在玩某种游戏,您可能应该改用class,这对于为用户存储信息更有意义。

您还专门检查isdigit(),但可以改用.isalpha() [docs]

检查整个字符串中的字母字符

答案 1 :(得分:0)

  • 创建一个接受文本并输出不带数字的字符串的函数-重复该操作直到用户配合为止(干-不要重复自己-您需要两次两次功能,因此使其成为一个函数
  • 将函数重命名为它们的作用func1 / func2错误函数名
  • 从第二个函数调用该函数,并从第二个函数返回两个名称

def get_string_no_numbers(text):
    while True:
        k = input(text)
        if any(str.isdigit(x) for x in k):
            print("No numbers allowed - try again!")
        else:
            return k

def get_first_name():
    user_name = get_string_no_numbers("What's your first name? ")
    return user_name

def get_full_name():
    user_name = get_first_name() # func1 is called only once
    last_name = get_string_no_numbers("What's your last name {}? ".format(user_name))
    return user_name, last_name

first_name, last_name = get_full_name() # decompose returned tuple
print(first_name)
print(last_name)

输出:

What's your first name? 
Jon21
No numbers allowed - try again!
What's your first name? 
24
No numbers allowed - try again!
What's your first name? 
John
What's your last name John? 
Smith22
No numbers allowed - try again!
What's your last name John? 
Smith

John
Smith

函数是一等公民,并且还可以具有属性(在您的情况下,这不是必需的,但可以)-您可以在“函数中”存储一些东西:

def f1():
    # store the input as attribute of the function
    f1.some_var = input()       

def f2():
    print(f1.some_var)

# f2() ->f1() not run yet: AttributeError: 'function' object has no attribute 'some_var'

f1()  # creates the attribute on f1, input is: Jon21    
f2()  # prints Jon21 

更多有关输入验证的信息:Asking the user for input until they give a valid response

答案 2 :(得分:0)

如果您想使代码在当前代码中不做任何更改,而只在中间添加1行,则在func1定义之后看看下面的代码。

  

在Python中,函数是第一类对象,因此您可以将1个函数分配给任何变量,作为参数发送等。例如:f = f2; f(); f2() | f = f2; f3(f, f2)

def func1():
    user_name = input("What's your name? ")

    if any(char.isdigit() for char in user_name):
        print("You can't put a number in your name.")
        sys.exit()
    else:
        pass

    return user_name

first_name_information = func1

def func2():
    user_name = first_name_information()
    last_name = input("What's your last name {}? ".format(user_name))

    if any(char.isdigit() for char in last_name):
        print("You can't put a number in your last name.")
        sys.exit()
    else:
        # print("So, you are {0} {1}".format(user_name, last_name))
        pass

func2()
  

输出:

What's your name? Rishikesh
What's your last name Rishikesh? Agrawani
  

输出 :(当您取消注释func2()中的注释行时)。我添加了其他内容,使执行对我而言几乎没有意义。

What's your name? Rishikesh
What's your last name Rishikesh? Agrawani
So, you are Rishikesh Agrawani