Python Forum
calling a function and argument in an input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling a function and argument in an input
hi I was wondering if anyone could help me with this for a personal prject I am working on
what I want to do is to call a function and an argument in a single input.
could you help I cant find anything online

it would look something like this:
>you rotated 6° to the right
Your description is very limited, but i assume you mean this:

somewhere in your app you have:
Quote:def rotate(angle):
rotate is a function and angle the argument.
Elsewhere you have an input() statement for the user,
and you want him/her to be able to type ">rotate(6)".
The simplest way would be to have two inputs:
a = input("What angle do you need ? ") -> convert that to int()
c = input("What command should I execute? ")
For the variable c you will have to construct some logic that says:
if it's "r" do rotate, if its "x" do something else.
My 2 cts,
Some extra thoughts on this:
If you are considering to use the split() function,
you might be better off reformatting the input string to "rotate,5"
It's less work, and you will need some extra logic, assuming that there are more commands than just rotate.

There is however another possibility, that does what you want, but it will probably require some rethinking of the function.
Consider this:
def add(x):
    print(x + x)
z = input('??? ')
So, if you input 'add(10) ', it will print "20".
This is a potentially dangerous thing, as you allow the user to execute all kinds of commands
that may not be desirable Rolleyes
The extremely dangerous way to do it is the exec() function. You are opening up your machine to the user, but here's an example
cmd = input('What do you want to do?')
print(f'You want to {cmd}')
What do you want to do?print('foo bar') You want to print('foo bar') foo bar

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use a tuple as an argument of a function zarox 5 177 Nov-14-2020, 08:02 PM
Last Post: buran
  Passing argument from top-level function to embedded function JaneTan 2 187 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  Use of input function to change screen background color in Turtles Oldman45 3 511 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  return string from function with one argument jamie_01 2 394 May-28-2020, 11:07 PM
Last Post: menator01
  how to use getter as argument in function nanok66 3 489 May-13-2020, 09:15 AM
Last Post: nanok66
  Getting an error while using input function dcsethia 5 411 May-11-2020, 04:59 PM
Last Post: buran
  Cant define turtle color with function argument Wrightys99 2 373 Apr-22-2020, 01:43 PM
Last Post: Wrightys99
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 443 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 379 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Taking Multiple Command Line Argument Input bwdu 6 615 Mar-29-2020, 05:52 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020