I'm making an atomic mass calculator

Hello, I'm a beginner in MITT and I was developing a molar mass calculator. I have two problems (that have appeared so far):

When I put "H20" in the formula, this error appears: "The * operation cannot accept arguments: , [""], [true]"
When I put only "H", this appears: "Select item from list: Attempting to get item number 2 from a list of length 1: ["H"]"

I would be extremely grateful if you could shed some light on my errors, below are the blocks, I thank you in advance.

Your comparisons are wrong:

Comparing two constants will always return a constant true/false result, so don't even bother.

You don't need to make separate variables for every Element name.

This should be done as a table, with Element names in column 1 and their atomic weights in column2, to take advantage of the AI2 list block Lookup In Pairs.

You can use Google Sheets to type this up quickly, and then download it as a CSV (Comma Separated Values) file, to upload into the Media Folder for a runtime load at app startup.

That would reduce your block count by a factor of at least 20.

Come back when you have done all that.

1 Like

How are you putting H20 into your formula? You don't seem to have a variable of that name.

'H' is an item in your List 'Listaelementos'. The error message however is saying you are trying to get a non-existent item from a List. If 'FormulaList' is supposed to be a List, it should be initalised with a "create empty list" Block, not a text Block.

Okay, I did what you said, now what?

image

This code will break up your formula into a list, the first step in calculating a weight.
You need to add a pass that will insert a default 1 for elements lacking a number.


molar_parser.aia (3.3 KB)

1 Like

Got it, now that I've done that, what's the next step?

Do you have a table loaded, ready for lookup in pairs, with the one or two letter atom names in column 1 and the weight in column 2?

oh, yes

Have you learned how to split the formula into a list of either element names or atom counts, like in my sample?

Learn is definitely a strong word, but I could understand the example very well.

Oh, I just realized, my splitter did not take into account atom counts greater that 9, and would separate digits into list items. Those would need to be combined, in the same pass where I recombine multiletter atom names.

(my dinner awaits, so I leave that to you.)

Alright, hahaha, thanks brother

After you fix that, do another pass backwards from the end to index 2, looking for either pairs of atom names followed by a number, or solitary atom names.

Use a running total, and apply your lookup and multiplication to get a number to add to the running total.

After that, is it over?

I was off by an hour on the dinner, so here's progress:
image


molar_parser.aia (3.4 KB)
This version works with Ice-9.

Depends on how advanced your formulas are.

This does not cover ionization or isotopes or parentheses.

But it's a start.

1 Like

This should do it, if you plug in your lookup table.
image


molar_parser.aia (5.0 KB)

1 Like

Sorry for the question, but... I don't know how to implement table elements in blocks, I'm really a beginner. I already have it exported in the media

Upload it here?

This?
Elements.csv (332 Bytes)

(The donkey forgot about He)