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.