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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With