0

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

dar
  • 5
  • 3
  • 1
    @SurajM completely wrong, especially considering there are already 3 different answers on how to achieve this – Nordle Mar 13 '19 at 11:13

4 Answers4

1

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.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • This worked, thanks! I think I will create a dictionary before release but this definitely helps with the alpha. – dar Mar 13 '19 at 12:55
0

make it a dictionary

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

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

print('your colour: ', colour, 'is number: ', colour_dict[colour])
Erik K
  • 471
  • 5
  • 12
0

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 
Usman
  • 1,983
  • 15
  • 28
  • If OP will have 10 colors do you suggest to have 10 if statements? – DeepSpace Mar 13 '19 at 11:13
  • 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. – Usman Mar 13 '19 at 11:15
0

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.

Zhenhir
  • 1,157
  • 8
  • 13