Sanstheskelet0n
Sanstheskelet0n

Reputation: 21

Transferring variables between functions Python

def name():
        name=input("what is your name?")
        print()
def print():
        print(name)

Hello new to programming!

I'm making a simple game and need to transfer names and scores through different functions much like the code displayed. Is there a way to make this work (Python)

Upvotes: 1

Views: 61

Answers (1)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

If you want a value from a function the clearest and most standard way to do it is to return the value:

def ask_name():
    name = input("what is your name?")
    return name # this gives the value back to the calling function

def another_function():
    name = ask_name() # this assigns the returned value to the variable "name"
    print(name)

Also, don't define functions with the same name as built-in functions like print():

def print(name_to_print):
    print(name_to_print)

If you call this function it will not print anything. Rather, when it calls print(name_to_print) it will call itself, not the built-in print(). Then that will call it self again over and over again until your program fails with the error "RecursionError: maximum recursion depth exceeded".

Upvotes: 2

Related Questions