Home > Mobile >  How to transform the string of this input into numbers?
How to transform the string of this input into numbers?

Time:01-30

from math import *

print("force  a to b")
Fa = input("Force a : ")
Fb = input("Force b : ")
distance = input("distance : ")

k = 9*10**9
Fa = float(Fa)
Fb = float(Fb)
distance = float(distance)

def force(Fa, Fb, distance):
    force = k * (Fa * Fb) / (distance)**2
    print(force)

force(Fa, Fb, distance)

The error I get:

script> python force.py
force a to  b
Force a : 1,6*10**-19
Force b :  -1.6*10**-19
distance : 0.053*10**-9
Traceback (most recent call last):
  File "C:\Users\rakot\Documents\math\script\force.py", line 9, in <module>
    Fa = float(Fa)
ValueError: could not convert string to float: '1,6*10**-19' 

I guess these kinds of numbers "1, 6*10**-19" are not float numbers — so my question is what types of numbers are they and how do I transform the string of the input into a type that allows me to do mathematical operations?

CodePudding user response:

That's because you are not entering the numbers correctly.

in your input try: 1.6e-19 instead of 1,6*10**-19

CodePudding user response:

input() always returns a string. In the forth line you also get a string. You can count what's inside it using eval() function: Fb = eval(input("Force b : "))

Don't use ',' in float, but '.', otherwise there will be an error

CodePudding user response:

print("force  a to b")
Fa = float(input("Force a : "))
a = float(input("Force a : "))
Fb = input("Force b : ")
b = input("Force b : ")
distance = input("distance 1: ")
distance2 = input("distance 2: ")
distance3 = input("distance 3: ")

k = 9*10**9
Fa = Fa*10**a
Fb = Fb*10**b
distance = distance*disrance2**distance3

def force(Fa, Fb, distance):
force = k * (Fa * Fb) / (distance)**2
print(force)

force(Fa, Fb, distance)

you could do this instead, and use . instead of ,

  •  Tags:  
  • Related