-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoppler Effect.py
125 lines (88 loc) · 5.09 KB
/
Doppler Effect.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import math
speed_of_sound = 331.0
def source_towards(frequency, velocity):
step1 = speed_of_sound - velocity
step2 = speed_of_sound / step1
final_frequency = step2 * frequency
final_frequency = str(final_frequency)
print("\n The observer will experience a frequency of " + final_frequency + " Hz when the source moves towards it.")
def source_away(frequency, velocity):
step1 = speed_of_sound + velocity
step2 = speed_of_sound / step1
final_frequency = step2 * frequency
final_frequency = str(final_frequency)
print("\n The observer will experience a frequency of " + final_frequency + " Hz when the source moves away from it.")
def observer_towards(frequency, velocity):
step1 = speed_of_sound + velocity
step2 = step1 / speed_of_sound
final_frequency = step2 * frequency
final_frequency = str(final_frequency)
print("\n The observer will experience a frequency of " + final_frequency + " Hz when the observer moves towards the source.")
def observer_away(frequency, velocity):
step1 = speed_of_sound - velocity
step2 = step1 / speed_of_sound
final_frequency = step2 * frequency
final_frequency = str(final_frequency)
print("\n The observer will experience a frequency of " + final_frequency + " Hz when the observer moves away from the source.")
def isDigit(x):
try:
float(x)
return True
except ValueError:
return False
while True:
relative_position = input("\n\n In this scenario, is there a moving source or a moving observer (type 'source' or 'observer')?\n\n ")
if relative_position == 'source':
approach = input("\n Is the source moving towards the observer or away from the observer (type 'towards' or 'away')?\n ")
if approach == 'towards':
frequency_source_string = input("\n What is the frequency (in Hz) of the moving source?\n ")
velocity_source_string = input("\n What is the velocity (in m/s) of the moving source?\n ")
if not isDigit(frequency_source_string):
print("\n Please input a number.\n")
elif not isDigit(velocity_source_string):
print("\n Please input a number.\n")
else:
frequency_source = float(frequency_source_string)
velocity_source = float(velocity_source_string)
source_towards(frequency_source, velocity_source)
elif approach == 'away':
frequency_source_string = input("\n What is the frequency (in Hz) of the moving source?\n ")
velocity_source_string = input("\n What is the velocity (in m/s) of the moving source?\n ")
if not isDigit(frequency_source_string):
print("\n Please input a number.\n")
if not isDigit(velocity_source_string):
print("\n Please input a number.\n")
else:
frequency_source = float(frequency_source_string)
velocity_source = float(velocity_source_string)
source_away(frequency_source, velocity_source)
else:
print("\n Please input 'towards' or 'away'.\n")
elif relative_position == 'observer':
approach = input("\n Is the observer moving towards the source or away from the source (type 'towards' or 'away')?\n ")
if approach == 'towards':
frequency_source_string = input("\n What is the frequency (in Hz) of the moving observer?\n ")
velocity_source_string = input("\n What is the velocity (in m/s) of the moving observer?\n ")
if not isDigit(frequency_source_string):
print("\n Please input a number.\n")
elif not isDigit(velocity_source_string):
print("\n Please input a number.\n")
else:
frequency_source = float(frequency_source_string)
velocity_source = float(velocity_source_string)
observer_towards(frequency_source, velocity_source)
elif approach == 'away':
frequency_source_string = input("\n What is the frequency (in Hz) of the moving observer?\n ")
velocity_source_string = input("\n What is the velocity (in m/s) of the moving observer?\n ")
if not isDigit(frequency_source_string):
print("\n Please input a number.\n")
if not isDigit(velocity_source_string):
print("\n Please input a number.\n")
else:
frequency_source = float(frequency_source_string)
velocity_source = float(velocity_source_string)
observer_away(frequency_source, velocity_source)
else:
print("\n Please input 'towards' or 'away'.\n")
else:
print("\n That is neither a moving source or observer. Please try again.\n")