Concatenating consecutive integers from input with digits and letters

Hello all,

So I have a program where I am trying to calculate the molecular weight of a chemical compound from user input. So far the program works, except when I have a code that has two integers i.e. H14 the program only calculates the “1”. I’ve attached a link to my program!


There’s a lot of repeated code there, and it’s very deeply nested.
Try to do one thing at a time instead.
I’d split the string at each letter C H4 O2 C H3 O2, then make a function that looks at one such group and figures out what the letter is and what the number is, then you can call that function on each group to get (C, 1), (H, 4), (O, 2), … after which you can look up the value and multiply by the number. In particular, note that each step here is only concerned with a small part of the problem - break it down into smaller steps.

A very nasty complicated thing you’re doing is user input, you may want to instead assume that you have a valid input, or make a function that accepts a string (user input has already happened in a separate step) and returns the result. User input is a separate problem, again, break it down into smaller steps and solve one problem at a time.


So I am in a class for python and we haven’t used regex. My understanding on it is minimal. However, If I were to use it, to split re.split(’\w’, str) would do just that, right? Or do you mean I should use the enumerate/zip function (not quite sure how to use this either).


Didn’t mention regex, you can use loops, lists, if statements, the only “special” thing you need is to be able to do is tell letters and digits apart but this can also be done using simple things like defining all numbers/letters and comparing or looking in your dict where you’ve already defined the valid letters. Using a search engine you can also easily find how to tell whether a character is letter/digit/other

So, maybe ignore things you’ve heard exist and instead think about what needs to happen and then put that together using basic operations.

One way you can go about splitting something is to keep two lists, one for the final result and one where you put in the “current” group’s characters. Each time you find the place to split you add that current group to the result, clear out the current one, and keep going with the rest.

\w doesn’t seem to be a suitable pattern here, no. That matches everything in your string.

Your function might behave something like:

>>> group('ab1c111')
[['a'], ['b', '1'], ['c', '1', '1', '1']]

From there you can make an entirely separate function which cleans that up a bit more, into something like:

[['a', 1], ['b', 1], ['c', 111]]

That function only needs to operate on a single group at a time, call it once on each group to process all of them:

>>> cleanup(['a'])
['a', 1]
>>> cleanup(['c', '1', '1', '1'])
['c', 111]
>>> # use map or a loop to apply your function to each value:
>>> map(cleanup, group('ab1c111'))
[['a', 1], ['b', 1], ['c', 111]]