Reputation: 25
G = float(6.67 * (10**-11))
print("Choose from the following '\n a) Gravitational Force '\n b) Density '\n c) Kinetic Energy '\n d) Potential Energy '\n e) Work Done '\n f) Weight '\n g) Force ")
Answer = input("Choice: ")
while Answer == 'a':
M1 = float(input("Enter mass of first body: "))
M2 = float(input("Enter mass of second body: "))
r = float(input("Enter distance between them: "))
break
while Answer == 'b':
vol = float(input("Enter volume of the object: "))
m = float(input("Enter mass of the body: "))
break
while Answer == 'c':
m1 = float(input("Enter mass of the body: "))
v = float(input("Enter velocity of the body: "))
break
while Answer == 'd':
g = float(input("Enter value of acceleration due to gravity: "))
m2 = float(input("Enter mass of the body: "))
h = float(input("Enter height of the object: "))
break
while Answer == 'e':
f = float(input("Enter vaue of force applied: "))
s = float(input("Enter value of displacement: "))
break
while Answer =='f':
g1 = float(input("Enter value of acceleration due to gravity: "))
m3 = float(input("Enter mass of the body: "))
break
while Answer == 'g':
m4 = float(input("Enter mass of the body: "))
acc = float(input("Enter value of acceleration: "))
break
D = m/vol
W = m3*g1
F = m4*acc
GF = (G * M1*M2)/r**2
WD = f*s
KE = (1/2 * m1 * v**2)
PE = m2*g*h
if Answer == 'a':
print(GF)
elif Answer == 'b':
print(D)
elif Answer == 'c':
print(KE)
elif Answer == 'd':
print(PE)
elif Answer == 'e':
print(WD)
elif Answer == 'f':
print(W)
elif Answer == 'g':
print(F)
Now I want to know that while running the code, it keeps saying variable not declared and it says that in the formulas the variable I am using are not defined but I already defined them in while loop, I want to bring only certain inputs when the user chooses a certain calculation. Please help! What are things I am doing wrong? please help Thanks
Upvotes: 0
Views: 53
Reputation: 71
Just do the specific calculations in the if clauses. The problem with your code is that certain variables don't get defined if the user is not choosing the corresponding options. So the calculations will fail.
e.g.
if Answer == 'a':
GF = (G * M1*M2)/r**2
print(GF)
This will make sure all neccessary variables are defined at the point where they are needed to be.
Upvotes: 2