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
  difference between forms of input a list to function akbarza 6 928 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  mutable argument in function definition akbarza 1 425 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 767 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,776 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Showing an empty chart, then input data via function kgall89 0 943 Jun-02-2022, 01:53 AM
Last Post: kgall89
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  input function question barryjo 12 2,636 Jan-18-2022, 12:11 AM
Last Post: barryjo

Forum Jump:

User Panel Messages

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