Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trouble printing answer to question but find nothing wrong in script?

I am making a fun little project with some useful functions and I was creating an Ask function where you enter what you want to ask and it gets input from the user and sets the variable answer to the answer the user gives. My only problem is I am printing the answer outside of the function but for some reason it prints nothing.

answer = " "   # set empty in the start
def ask(question):
    answer = input(question) # sets the answer to the user's input
ask("how are you ")
print(answer)  # ends up printing nothing.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You must print answer:

  1. inside of ask function,
    or
  2. return answer, catch it and then print it

answer = " "   # set empty in the start
def ask(question):
    answer = input(question)
    return answer
    
answer = ask("how are you ")
print(answer)

or one line:

print(ask("how are you "))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading