0

I'm trying to make a program that will output an answer based on the formula for universal gravitation. I am completely new to python so I have no idea what I'm doing. I imagine I must define a function with inputs for each of the 3 variables. But then how do I write it so that the user can input each of the variables to plug into the formula? Also, every time I try to input variables I get "can't multiply sequence by non-int of type 'float'".

What I have right now:

def force(m,M,d):
  answer = (G*m*M)/(d**2)
  return answer
n= int(input("1: F 2: d 3: m(1 or 2). Solve for: "))
if n==1:
  m = input("Enter m: ")
  M = input ("Enter M:")
  d = input ("Enter d:")

  print (force(m,M,d))

  G= 6.674*10**(-11)

Please help!

hjg
  • 1
  • 2
  • `input()` returns a string value. It has to, because the user can obviously type anything they like, so a string is the only data type that can faithfully represent the potential range of input values. If you want to convert the input to an integer or floating point value, use `int()` or `float()`. – John Gordon Sep 28 '19 at 02:42
  • 1
    Similar to how you forced the input for the variable `n` to an integer with the int() function, you need to do something similar for the other variables m, M, and d you have. Otherwise, they will be treated as strings, which is the error that you're getting. – Eric Leung Sep 28 '19 at 02:43
  • 1
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) - The top answer is pretty verbose so I'd recommend [this answer](https://stackoverflow.com/a/20449436/4518341) just to get the gist. And tl;dr use `float(input(...` – wjandrea Sep 28 '19 at 02:47

2 Answers2

0

The values of variable m and M should be of floating type, because they denote masses of two bodies. You were getting error because you were trying to multiply a string value(earlier m and M) with a floating type (G).

def force(m,M,d):
  answer = (G*m*M)/(d**2)
  return answer
n= int(input("1: F 2: d 3: m(1 or 2). Solve for: "))
if n==1:
  m = float(input("Enter m: "))  # change this
  M = float(input ("Enter M:"))  # and this 
  d = float(input("Enter d:"))   # and this as well

  print (force(m,M,d))
G= 6.674*10**(-11)
taurus05
  • 2,491
  • 15
  • 28
0
import math

G = 6.674*(10**(-11))

def force(m1, m2, d):
  F = (G*m1*m2)/(d**2)
  return F


def distance(m1, m2, F):
  d = math.sqrt( (G*m1*m2)/F )
  return d


def mass(m1, F, d):
    m2 = (F*(d**2)) / (G*m1)
    return m2


def get_input(input_request):
    try:
        out = float(input(input_request))
    except:
        print('please only enter numeric values.')
    return out


n = input("Solve for force, distance, or mass. Input one: ")

if n == 'force':
    m1 = get_input("Enter m1: ")
    m2 = get_input("Enter m2: ")
    d = get_input("Enter d: ")
    print(force(m1, m2, d) )
elif n == 'distance':
    m1 = get_input("Enter m1: ")
    m2 = get_input("Enter m2: ")
    F = get_input("Enter F: ")
    print(distance(m1, m2, F))
elif n == 'mass':
    m1 = get_input("Enter m1: ")
    F = get_input("Enter F: ")
    d = get_input("Enter d: ")
    print(mass(m1, F, d))
else:
    print("Please enter: 'force', 'distance', or 'mass'")
AidanGawronski
  • 2,055
  • 1
  • 14
  • 24