-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40_law_of_gravity.py
107 lines (92 loc) · 3.65 KB
/
40_law_of_gravity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#We need to set initial values to our flags otherwise the while loop won't know
#what to do (and we'll get a runtime error)
planet_incorrect = True
distance_incorrect = True
#We want this condition to be or, because we want it to restart if either the
#planet or the distance is incorrect
while planet_incorrect or distance_incorrect:
#Choose one of the 8 planets
planet = input("Which planet do you want to calculate for? ")
#Choose either "Close" or "Far"
distance = input("Do you want to calculate the Close or Far disntace? ")
#It is still easier to check if the distance is correct at the beginning
#since it will mean less lines of code than doing it in the if/else statements
if distance == "Close" or distance == "Far":
distance_incorrect = False
else:
#We should print(an error message letting the user know what they did wrong
print("You have entered an invalid distance, please try again")
distance_incorrect = True
#Check if the planet is Jupiter and set the necessary values
if planet == "Jupiter":
planet_incorrect = False
m_2 = 1.8986*(10**27)
if distance == "Close" :
d = 741*(10**9)
elif distance == "Far":
d = 817*(10**9)
#Check if the planet is Saturn and set the necessary values
elif planet == "Saturn":
planet_incorrect = False
m_2 = 5.6846*(10**26)
if distance == "Close" :
d = 1.35*(10**12)
elif distance == "Far":
d = 1.51*(10**12)
#Check if the planet is Neptune and set the necessary values
elif planet == "Neptune":
planet_incorrect = False
m_2 = 10.243*(10**25)
if distance == "Close" :
d = 4.45*(10**12)
elif distance == "Far":
d = 4.55*(10**12)
#Check if the planet is Uranus and set the necessary values
elif planet == "Uranus":
planet_incorrect = False
m_2 = 8.68*(10**25)
if distance == "Close" :
d = 2.75*(10**12)
elif distance == "Far":
d = 3*(10**12)
#Check if the planet is Earth and set the necessary values
elif planet == "Earth":
planet_incorrect = False
m_2 = 5.9736*(10**24)
if distance == "Close" :
d = 147*(10**9)
elif distance == "Far":
d = 152*(10**9)
#Check if the planet is Venus and set the necessary values
elif planet == "Venus":
planet_incorrect = False
m_2 = 4.8685*(10**24)
if distance == "Close" :
d = 107*(10**9)
elif distance == "Far":
d = 109*(10**9)
#Check if the planet is Mars and set the necessary values
elif planet == "Mars" :
planet_incorrect = False
m_2 = 6.4185*(10**23)
if distance == "Close" :
d = 205*(10**9)
elif distance == "Far":
d = 249*(10**9)
#Check if the planet is Mercury and set the necessary values
elif planet == "Mercury":
planet_incorrect = False
m_2 = 3.3022*(10**23)
if distance == "Close" :
d = 46*(10**9)
elif distance == "Far":
d = 70*(10**9)
#If none of the others were correct, then we have an incorrect input, so we should
#update planet_incorrect to reflect that
else:
print("You have entered an invalid planet, please try again")
planet_incorrect = True
m_1 = 1.9891*(10**30) #The mass of the Sun
G = 6.673*(10**(-11)) #The gravitational constant
F = (G*m_1*m_2)/(d**2) #Calculate the force
print(F) #Print the value found