1

I'm currently working my way through "Learn Python the Hard Way" and got to the first exercise about functions. It's simply creating a few functions and printing them out like in the previous examples in the book`.

Code:

def print_two(*args):
    arg1, arg2 = args
    print("arg1: %r, arg2: %r" % (arg1, arg2))

def print_two_again(arg1, arg2):
    print("arg1: %r, arg2: %r" % (arg1, arg2))

def print_one(arg1):
    print("arg1: %r" % (arg1))

def print_none():
    print("I got nothing.")

print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

Output in cmd:

C:\Users\[USER]\Google Drive\Python\Learn Python the Hard Way>python ex18.py
arg1: 'Zed', arg2: 'Shaw'
arg1: 'Zed', arg2: 'Shaw'
arg1: 'First!'
I got nothing.

I want to play around a bit with this, so instead of just giving me the above four lines when I run it, I want to be able to input the name of the function and then return the result. I tried with the following, but maybe I just don't understand how Python works?

x = input("> ")
print(x)

I'm not quite sure on the terminology but it would give me the following in cmd:

C:\Users\[USER]\Google Drive\Python\Learn Python the Hard Way>python ex18.py
> print_none()  # This is something I write myself
I got nothing.
5
  • Return a result of a method call? Commented Jul 1, 2018 at 14:38
  • I tried to change the question, does it make sense now? Commented Jul 1, 2018 at 14:47
  • 1
    Yeah, so you'd like to specify a method name and then execute it and print a result to the console. Commented Jul 1, 2018 at 14:49
  • Yeah, that´s what I'm trying to do. Commented Jul 1, 2018 at 14:52
  • Thank you, I realized that I should have posted the answer below instead. Commented Jul 2, 2018 at 10:00

2 Answers 2

1

The question might have been a little vague, but I found a solution myself. What I was looking for was the "import" function of the script into cmd, which then allows input the commands and return the lines that were hardcoded before:

C:\Users\[User]\Google Drive\Python\Learn Python the Hard Way>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex18
arg1: 'Zed', arg2: 'Shaw'
arg1: 'Zed', arg2: 'Shaw'
arg1: 'First!'
I got nothing.
>>> ex18.print_none()
I got nothing.
>>>
Sign up to request clarification or add additional context in comments.

Comments

0
def print_two_again(arg1, arg2):
    print("arg1: %r, arg2: %r" % (arg1, arg2))

def print_one(arg1):
    print("arg1: %r" % (arg1))

if __name__ == '__main__':
    x = input("F name:")
    eval(x)

Example:

/home/denis/zmqPG/bin/python /home/denis/PycharmProjects/useless/so2.py
F name:print_one(5)
arg1: 5

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.