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

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.