-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge.py
81 lines (70 loc) · 2.2 KB
/
Challenge.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
#Solution of the ComputerWomen challenge for FemHack by Nuwe
#Author: Rocio Gonzalez Toral / [email protected]
import math
import sys
import json
g=9.807
#Compute the maximum height of the projectile (h_max)
def maximum_height(vi):
h_max = ( vi * vi ) / ( 2 * g )
return h_max
#Compute the maximum traveled distance
def maximum_traveled(vi, alph):
x_max = 2 * vi* math.sin(alph) / g
return x_max
#Save the computed data (Inputs + Results) into a file
def save_data(d1, d2, r1, r2):
try:
f = open ("results.txt","w")
f.write("|Inputs:")
f.write(" Initial velocity: " + str(d1))
f.write(", Launch angle: " + str(d2))
f.write("| Results:")
f.write(" Maximum height of the projectile: " + str(r1))
f.write(", Maximum traveled distance: " + str(r2))
f.write("|")
f.close()
print("Inputs and Results are saved in results.txt")
except:
print("Something go wrong with save file")
sys.exit(1)
#Create a Json File with input data
def input_json():
# Data to be written
dictionary = {
"v0" : 1.0,
"alpha" : 30.0,
}
with open("inputdata.json", "w") as outfile:
json.dump(dictionary, outfile)
#Introduction data
option = int(input("Select the way to introduce the data (1:JSON/2:Manual)"))
if (option == 1):
#JSON
#read from json
nameJson=str(input("Name the Json file: "))
# Opening JSON file
try:
f = open(nameJson,'r')
# returns JSON object as a dictionary
data = json.load(f)
v0=data['v0']
alpha=data['alpha']
# Closing file
f.close()
except Exception as e:
print(e)
sys.exit(1)
elif (option == 2):
#manual
v0=float(input("Initial velocity: "))
alpha=float(input("Launch angle: "))
else:
print("Just 1 or 2 option")
sys.exit(1)
fileresult=str(input("Do you want save the results in a file(s/n)?: "))
if fileresult=="s" or fileresult=="S":
save_data(v0,alpha,maximum_height(v0),maximum_traveled(v0,alpha))
else:
print(f"The Maximum height of the projectile: " + str(maximum_height(v0)))
print(f"Maximum traveled distance: " + str(maximum_traveled(v0,alpha)))