[Tutor] Is there a better way to use scientific notation in an equation?

Steven D'Aprano steve at pearwood.info
Mon May 3 00:55:26 CEST 2010


On Mon, 3 May 2010 06:44:42 am David Hutto wrote:
> In the following code I'm trying to do basic calculations with
> coulumb's law
>
> #Coulombs Law
> '''
> F = (9*(10**9)) * (Q1*Q2) / (d**2)
> '''
> base = 10
> Q1mult = raw_input('First enter multiplier of base 10
> charge/coloumb(Q1):') Q1exp = raw_input('Now enter exponent of base
> 10(Q1):')
> Q1 = int(Q1mult)*(10**int(Q1exp))
> Q2mult = raw_input('First enter multiplier of base 10
> charge/coloumb(Q2):') Q2exp = raw_input('Now enter exponent of base
> 10(Q2):')
> Q2 = int(Q2mult)*(10**int(Q2exp))
> d = raw_input('Enter distance of charges/coulumbs from Q1 to Q2:')
>
>
>
> a = (9*(10**9))*((int(Q1))*(int(Q2)))/((int(d))**2)
> print a
> **********************************************************
>
> Q1 and Q2 are to be entered as base ten scientific notation.
> When I try to input Q1 as raw input, entering in ((2*(10**7)), I get:
>
> ValueError: invalid literal for int() with base 10: '((2)*(10**7))'

((2)*(10**7)) is not an integer, it is a mathematical expression. 
Integers look like:

1
-27
37481000005

and similar. Apart from a leading minus sign to make negative numbers, 
only digits are allowed.

The way to enter numbers in scientific notation is to use floats, in 
exponential form, just like scientific calculators:

2e7

and to change your program to use float instead of int. Also, you have a 
lot of unnecessary brackets that don't do anything except make it hard 
to read. This should do it:

a = 9e9*float(Q1)*float(Q2)/float(d)**2



-- 
Steven D'Aprano


More information about the Tutor mailing list