How do I use the input function to call the value of a variable?

how are variables passed as input to a function
how to input variable in function matlab
input function python 3
matlab call variable from another function
call a variable from a function
return variables from function matlab
matlab input function
prompt in python

Original Question

This is written in Python 2 using Jupyter Notebook. I apologize in advance if the phrasing/terms are unconventional. I am a beginner & unaware of most coding conventions.

blue = 24
red = 18

color = input('What color would you like to know the value for? \n')
print (color)

I would like to be able to run the code, input 'blue' and have it print 24.


Ideally, I would like to not have to build a dictionary. This is a simplification of my actual code. I already have many, many variables defined (some definitions requiring user input) and it would be very inconvenient to have to create a dictionary from which.

This is my (more realistic) code:

blue = input('What is the value of blue?')
green = input('What is the value of blue?')
cyan = blue*green

color = input('What color would you like to know the value for? \n')
print (color)

I would like to be able to run the code, input cyan and have it print the numerical value of the variable cyan.

example: run. input 3. input 5. input cyan. code prints 15

Use raw_input. input will override the existing value in the good case, and in a worse case raise a NameError. Then try to retrieve the value from locals():

blue = 24
red = 18

color = raw_input('What color would you like to know the value for? \n')
   print locals()[color] 
except KeyError:
   print 'Color {} is not defined'.format(color)

Note that this is pretty much a hack, and using this way the user will be able to access any local variable defined. If you want to limit what the user can access then use a predefined dictionary like some of the other answers suggest.

How can I call a function that has an input variable?, variable = input('Input variable: '); The above should be outside the function and then this can be passed as an argument to the function by calling , as simple as that. This should cause the function's input call to call this function instead. However, this would only work for always returning the same input to the function. You could make this function smarter, providing a return value based on the input to input, or use a persistent variable to track the number of times input was called or something.

make it a dictionary

colour_dict = {'blue': 24, 'red': 18}

colour = input('what colour...')

print('your colour: ', colour, 'is number: ', colour_dict[colour])

Variable name of function input - MATLAB inputname, First calling variable is 'x'. Call the function using values instead of variables. The inputname function returns an empty char array because its input does� Actually using document.myForm._fid_14.value=variable leaves the select box blank. I would use your example of using the index value, but I know the actual value of the select box, not the index.

Try this code ! You need to use conditional statement (if clause) to check that user input the value is blue. If its true then print the blue variable value.

Code :

blue = 24
red = 18

color = input('What color would you like to know the value for? \n')
if (color==blue):

Output :

What color would you like to know the value for?                                                                       

Python2 Tutorial: Keyboard Input, If the user e.g. puts in an integer value, the input function returns this integer Traceback (most recent call last): File "", line 1, in <module> name = input("What's We will use the function type to check the type of the variables: It requires a float type of pointer variable as its only argument. Line 10 passes the address of the price variable to the discount() function. The percent sign obtains the memory location of the price variable. Within the function, pointer variable a is used to peek at the value at the memory location that’s passed.

I'd use a dictionary for this

#!/usr/bin/env python

options = {"blue":24, "red":18}

answer = raw_input('What color would you like to know the value for? \n')


You could access the variable literally with eval or from locals. But that's generally considered a bad idea unless you actually have no other option. I'd also argue it's more complicated for no real reason.

An explanation of what's happening:

We create a new dictionary called options, with "blue" and "red" as it's keys and 24 and 18 as it's values respectively.

Now when we want our answer we can access it with options[answer] which is the same as options["blue"] since I gave "blue" as an answer to input.


What color would you like to know the value for? 


If you're running python 2 use raw_input instead of input. This is a very simple solution, and if the user inputs anything other than "blue" or "red" the script will raise a KeyError. I'd start with I'm getting Key error in python for notes on how to deal with that.

Variables and User Input, A Lua tutorial about 'Variables and User Input' Wherever you put the function call, the value that the user enters will take its place. So if you want to put � The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments. In general, it means the code within a

1.10. Input and Output — Hands-on Python Tutorial for Python 3, That is what the built-in function input does: First it prints the string you give as a parameter (in this case In the program this value is assigned to the variable person, for use later. First method calling syntax for objects is used . Otherwise - if you want to use jQuery rather than javascript in passing variables to an input of any kind, use the following to set the value of the input on an event click(), submit() et al: on some event; assign or set the value of the input: $('#inputid').val($('#idB').text()); where:

Get User Input from Keyboard - input() function, The input() function gets user input (keyboard) and stores it into variable name. So, to get a text value you can use the function input() which takes as input a string to be printed. If you call the input() function, it returns plain text (string). Can I use a variable inside of an input statement? [duplicate] Ask Question Asked 3 years, The current value of x will be inserted in the placeholder {}.

Parameter (computer programming), In computer programming, a parameter or a formal argument, is a special kind of variable, used In case of call by value, what is passed to the function is the value of the argument – for example, f(2) and a = 2; An input argument (the argument to an input parameter) must be a value, such as an initialized variable or literal� Hello. you can use the 'sim' function that you can write the inputs parameters of simulink in an mfile or workspace, then run the slx file with sim function. Cite 1 Recommendation

  • @SurajM completely wrong, especially considering there are already 3 different answers on how to achieve this
  • This worked, thanks! I think I will create a dictionary before release but this definitely helps with the alpha.
  • Since OP is using Python 2.X, raw_input should be used instead of input.
  • If OP will have 10 colors do you suggest to have 10 if statements?
  • nope. In that case, the best solution is to declare the dictionary & get the value of dictionary through its key. I give the solution according to the current situation.