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
#1
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:
input:
>rotate(6)
output:
>you rotated 6° to the right
Reply
#2
Your description is very limited, but i assume you mean this:

somewhere in your app you have:
Quote:def rotate(angle):
....do_something
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,
Paul
Reply
#3
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('??? ')
eval(z)
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
Paul
Reply
#4
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}')
exec(cmd)
Output:
What do you want to do?print('foo bar') You want to print('foo bar') foo bar
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input function cutting off commands at spaces. throwaway34 3 110 May-12-2021, 06:40 AM
Last Post: throwaway34
  How to input & output parameters from command line argument shantanu97 1 208 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Defining a function with input abcd 5 438 Feb-21-2021, 02:34 AM
Last Post: NullAdmin
  Calling Input for Random Generation ScaledCodingWarrior 1 207 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  Function with array input Gerkamspiano 5 391 Dec-01-2020, 08:48 AM
Last Post: Gerkamspiano
  How to use a tuple as an argument of a function zarox 5 500 Nov-14-2020, 08:02 PM
Last Post: buran
  Passing argument from top-level function to embedded function JaneTan 2 389 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  Use of input function to change screen background color in Turtles Oldman45 3 1,047 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  return string from function with one argument jamie_01 2 613 May-28-2020, 11:07 PM
Last Post: menator01
  how to use getter as argument in function nanok66 3 710 May-13-2020, 09:15 AM
Last Post: nanok66

Forum Jump:

User Panel Messages

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