python: call a function with parameter from input - python

I have a list of tuples consisting of name, phone number and address, and a function called "all" which just shows a list of all the tuples (like in a phonebook). The function is called via input from a user.
I want another function, called "entry" which shows a specific entry of my list. This function should be called via input as well with the index number of the entry (for example, "entry 12") and show just this entry.
Although I can't figure out how to take the number from the input as a parameter for my function and how to call the function. Does it have to contain a variable in the function name which will later be replaced by the number? How can i do that?

Have you looked into argparse?
import argparse
parser = argparse.ArgumentParser(description='your description')
parser.add_argument('-entry', dest="entry")
args = parser.parse_args()
print (args.entry)
You can then call this with python yourfile.py -entry="this is the entry"
That will allow you to take an input when you run the file.

I'm sorry if misunderstood your question, but it seems like you need function arguments. For example: if your `entry' program just prints out what the user put in, your code would look like this:
def entry(user_input): # the variable in the parentheses is your argument--is a local variable ONLY used in the function
print user_input # prints the variable
# now to call the function--use a variable or input() as the function argument
entry(input("Please input the entry number\n >>> ") # see how the return from the input() function call is used as a variable? this basically uses what the user types in as a function argument.
Try running it, and you'll see how it works.
Best of luck and happy coding!

Related

failing using def and input together

I'm a newbie to programming and trying to learn it with edx/microsoft.
I'm failing at this task somehow I don't understand it.
Define yell_this() and call with variable argument
define variable words_to_yell as a string gathered from user input()
Call yell_this() with words_to_yell as argument
get user input() for the string words_to_yell
I'm using python 3 on an azure jupyter notebook on linux
I'll try to answer to the best of my understanding of this question
Step 1.
define variable words_to_yell as a string gathered from user input()
In python you can define a variable like this:
words_to_yell = input('Please provide a word to yell: ')
This will define a variable containing whatever the user passed in
Step 2.
Call yell_this() with words_to_yell as argument
Assuming you have this function defined, you can call it like this
yell_this(words_to_yell)
#define the function and ask for user input
def words_to_yell():
words_to_yell = input("Please type words to yell:")
print(words_to_yell.upper() + "!!!")
#call the function like this:
words_to_yell()

Why does a function execute when put inside a variable (and how to stop it)

While writing a program in python i noticed that if one puts a function like print("hello world") inside a variable it will not be stored like expected, instead it will run. Also when i go and call the variable later in the program it will do nothing. can anyone tell me why this is and how to fix it?
If mean something like:
variable = print("hello world")`
then calling the function is the expected result. This syntax means to call the print function and assign the returned value to the variable. It's analogous to:
variable = input("Enter a name")
You're surely not surprised that this calls the input() function and assigns the string that the user entered to the variable.
If you want to store a function, you can use a lambda:
variable = lambda: print("hello world")
Then you can later do:
variable()
and it will print the message

Understanding python code?

I need some help understanding the start of this code:
def get_int_input(prompt=''):
I know what the int_input does but i need some help understanding the other parts of the line to finish my code.
I am assuming that by "other parts", you mean prompt=''.
prompt is a named parameter. Named parameters are given a default value whenever its function is called without passing a value to that parameter. Otherwise, it will use the value that was passed.
If you do:
>>> get_int_prompt()
Then the value of prompt (inside the function) would be an empty string ('').
However, if you do:
>>> get_int_prompt('What is your age? ')
Then the value of prompt (inside the function) would be 'What is your age? '.
Source: Python Central

Placing a Python variable inline

Forgive this rather basic Python question, but I literally have very little Python experience. I'm create a basic Python script for use with Kodi:
http://kodi.wiki/view/List_of_built-in_functions
Example code:
import kodi
variable = "The value to use in PlayMedia"
kodi.executebuiltin("PlayMedia(variable)")
kodi.executebuiltin("PlayerControl(RepeatAll)")
Rather than directly providing a string value for the function PlayMedia, I want to pass a variable as the value instead. The idea is another process may modify the variable value with sed so it can't be static.
Really simple, but can someone point me in the right direction?
It's simple case of string formatting.
template = "{}({})"
functionName = "function" # e.g. input from user
arg = "arg" # e.g. input from user
formatted = template.format(functionName, arg)
assert formatted == "function(arg)"
kodi.executebuiltin(formatted)
OK as far as I get your problem you need to define a variable whose value could be changed later, so the first part is easier, defining a variable in python is as simple as new_song = "tiffny_avlord_I_love_u", similarly you can define another string as new_video = "Bohemia_on_my_feet", the thing to keep in mind is that while defining variables as strings, you need to encapsulate all the string inside the double quotes "..." (However, single quotes also work fine)
Now the issue is how to update it's value , the easiest way is to take input from the user itself which can be done using raw_input() as :
new_song = raw_input("Please enter name of a valid song: ")
print "The new song is : "+new_song
Now whatever the user enters on the console would be stored in the variable new_song and you could use this variable and pass it to any function as
some_function(new_song)
Try executing this line and you will understand how it works.

Calling function from input variable

def function1(arguments):
print("Function 1",arguments)
def function2(arguments):
print("Function 2",arguments)
userInput = input()
Is it possible for the user to enter a function and arguments and for said function to run. eg the user enters function2("Hello World")
Though you can always use eval to make this work but for reasons eval is evil, it is better to use a dictionary call back mechanism, notably
You can create a dictionary to bind the function with the names and call them with appropriate parameters
call_backs = {'function1': function1, 'function2': function2}
assuming you provide an input as follows function2, "Hello World",
You first need to split the data userInput = userInput .split(',') and pass it onto the callback function via the dictionary
call_backs[userInput[0]](userInput[1])

Categories

Resources