1

I have func1 that contains a variable, and I want to access that variable in func2. I have tried in the code below to return the variable in func1 and then set variable "user_name" to the function "first_name_information", but this makes func1 run two times, which I don't want to happen.

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

hygull
  • 8,464
  • 2
  • 43
  • 52

3 Answers3

0

You could do this a few ways but probably this is good for your case, you just call func1 from func2:

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()

You will need to return from your second function and carry both values back, probably as a tuple.

It seems like you are making some sort of game, you probably should use a class instead, which would make more sense to store the information for your user.

Also you are checking specifically for isdigit() but you could instead check the entire string for alphabetic characters using .isalpha() [docs]

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
0
  • make a function that takes a text and outputs a string without numbers- let it repeat until the user cooperates (DRY - dont repeat yourself - you need this funcitonality twice so make it a function
  • rename your function to what they do func1 / func2 are bad function names
  • call the function from your second one and return both names from the second one

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)

Output:

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

Functions are 1st class citizens and can have attributes as well (in your case this is not needed - but possible) - you could store something "at the function":

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 

More info for input validation: Asking the user for input until they give a valid response

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

If you want to make your code work with no any changes in the current code and just by adding 1 extra line in middle then have a look at below code after the func1's definition.

In Python, functions are first class object so you can assign 1 function to any variable, send as arguments etc. For example like: f = f2; f(); f2() | f = f2; f3(f, f2) etc.

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()

Output:

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

Output: (when you uncomment the commented line in func2()). I have added it extra to make the execution little meaningful to me.

What's your name? Rishikesh
What's your last name Rishikesh? Agrawani
So, you are Rishikesh Agrawani
hygull
  • 8,464
  • 2
  • 43
  • 52