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
  [Solved] TypeError when calling function Laplace12 2 269 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Input function cutting off commands at spaces. throwaway34 3 392 May-12-2021, 06:40 AM
Last Post: throwaway34
  How to input & output parameters from command line argument shantanu97 1 534 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Defining a function with input abcd 5 705 Feb-21-2021, 02:34 AM
Last Post: NullAdmin
  Calling Input for Random Generation ScaledCodingWarrior 1 387 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  Function with array input Gerkamspiano 5 565 Dec-01-2020, 08:48 AM
Last Post: Gerkamspiano
  How to use a tuple as an argument of a function zarox 5 751 Nov-14-2020, 08:02 PM
Last Post: buran
  Passing argument from top-level function to embedded function JaneTan 2 580 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  Use of input function to change screen background color in Turtles Oldman45 3 1,392 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  return string from function with one argument jamie_01 2 807 May-28-2020, 11:07 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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