Reputation: 5
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.
EDIT:
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
Upvotes: 0
Views: 742
Reputation: 1240
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')
print(options[answer])
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.
Result:
What color would you like to know the value for?
blue
24
Notes:
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.
Upvotes: 0
Reputation: 81614
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')
try:
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.
Upvotes: 1
Reputation: 2029
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):
print(blue)
Output :
What color would you like to know the value for?
blue
24
Upvotes: 0
Reputation: 469
make it a dictionary
colour_dict = {'blue': 24, 'red': 18}
colour = input('what colour...')
print('your colour: ', colour, 'is number: ', colour_dict[colour])
Upvotes: 0