0

I have been self-teaching Python for a few weeks now and have the aim to create a script to run an equation and keep hitting walls. What I basically want to do is take an input with a unit attached i.e. 6M being 6,000,000, convert the unit into a numerical format and put that into an equation with an output.

So far I have defined a function:

def replaceunit(body):
    body = body.replace(str(body[-1]),str(units.get(body[-1])))
    return body

I have asked for the input and have a dictionary of units (shortened dictionary below):

    T = input("T = ")
    B = input("B = ")

    units ={'M': 1e6,    # mega
            'G': 1e9    # giga
    }

I then try and replace the if an M or G appears in the T or B variables:

    if str(T[-1]).isalpha() == True:
        replaceunit(T)
    if str(B[-1]).isalpha() == True:
        replaceunit(B)

After this I would like the updated T and B to be put into an equation that I define.

If I add a print action to my function I can see the values have been replaced, but have been unable to pull the corrected values through outside of the function and into another equation.

As I say, I'm very new to this, so if there's any help you can lend I'd very much appreciate it. Apologies also if this has been asked elsewhere, the few similar answers I have seen I haven't really understood the answer too.

1
  • 1
    Can you add code example of a simple "equation"? Commented May 19, 2020 at 15:29

2 Answers 2

1

Strings are immutable in Python, meaning they cannot be changed in place, but rather you have to create a new string for every change. That is exactly what you did in replaceunit - you wrote body = body.replace(...) and you replaced the old reference with a new one that replace gave you.

replaceunit is also returning a new reference, so calling it should be done as T = replaceunit(T) and B = replaceunit(B) to save changes. You must not use the same variable if you want to save both the replaced and non-replaced versions of the string.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want the value you returned from replaceunit to be the new value of T, you need to assign it:

T = replaceunit(T)

Note that you could skip the step of assigning body inside the function itself and simply return the value:

def replaceunit(body):
    return body.replace(str(body[-1]),str(units.get(body[-1])))

I would also suggest that it might be more useful to have a function that turns the user-inputted number into an actual number:

def parse_number(body: str) -> float:
    """Converts a string like '2G' into a value like 2000000000."""
    units ={
        'M': 1e6,    # mega
        'G': 1e9,    # giga
    }
    return float(body[:-1]) * units[body[-1]]

This will be necessary if you want to do any actual math with that value!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.