Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input variables in Python 3

Is it possible to have a user input equal to a variable for tasks that involve chemical elements.

For example, Carbon has the molecular mass 12, but i do not want the use to input 12, They should input 'C'. but as the input turns this into a string, it is not possible to lik this to the variable C = 12.

Is there any way to input a variable istead of a string?

If not, could i set a string as a variable.

example:

C = 12

element = input('element symbol:')
multiplier = input('how many?')

print(element*multiplier)

This just returns an error stating that you can't multiply by a string.

like image 833
Matt Avatar asked Dec 12 '22 18:12

Matt


1 Answers

You could change your code like this:

>>> masses = {'C': 12}
>>> element = input('element symbol:')
element symbol:C
>>> masses[element]
12
>>> multiplier = input('how many?')
how many?5
>>> multiplier
'5'                                          # string
>>> masses[element] * int(multiplier)
60
like image 181
SilentGhost Avatar answered Jan 04 '23 16:01

SilentGhost