-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic-progression-calculator.py
More file actions
153 lines (139 loc) · 5.03 KB
/
arithmetic-progression-calculator.py
File metadata and controls
153 lines (139 loc) · 5.03 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
print ("\nBem-vindo(a) à Calculadora de Progressão Aritmética do Raphael\n")
def readline(var, text):
while True:
try:
var = input(text)
if var != "s" and var != "m":
var = float(var)
return var
except:
print ("Entre com um número!")
def whole_sequence():
first_term, last_term, ratio = None, None, None
#Entrada
first_term = readline(first_term, "1º termo da progressão (a1): ")
if first_term == "s":
return 0
elif first_term == "m":
return 1
last_term = readline(last_term, "Último termo da progressão (an): ")
if last_term == "s":
return 0
elif last_term == "m":
return 1
ratio = readline(ratio, "Razão da progressão (r): ")
if ratio == "s":
return 0
elif ratio == "m":
return 1
# Saída:
print ("\nConjunto verdade para todos termos da seqûencia:\n{", end=''); print(first_term, end='')
first_term += ratio
while first_term <= last_term:
print ("; {}".format(first_term), end='')
first_term += ratio
print("}\n")
def find_term():
first_term, ratio, index = None, None, None
#Entrada
first_term = readline(first_term, "1º termo da progressão (a1): ")
if first_term == "s":
return 0
elif first_term == "m":
return 1
ratio = readline(ratio, "Razão da progressão (r): ")
if ratio == "s":
return 0
elif ratio == "m":
return 1
index = readline(index, "Índice do termo desejado (n de an): ")
if index == "s":
return 0
elif index == "m":
return 1
# Saída:
print ("\nan =", first_term + (index - 1) * ratio, "\n")
def find_ratio():
first_term, second_term = None, None
#Entrada
first_term = readline(first_term, "1º termo: ")
if first_term == "s":
return 0
elif first_term == "m":
return 1
second_term = readline(second_term, "2º termo: ")
if second_term == "s":
return 0
elif second_term == "m":
return 1
# Saída:
print ("\nr =", second_term - first_term, "\n")
def general_sum():
first_term, last_term, last_index = None, None, None
#Entrada
first_term = readline (first_term, "1º termo da progressão (a1): ")
if first_term == "s":
return 0
elif first_term == "m":
return 1
last_term = readline(last_term, "Último termo da progressão (an): ")
if last_term == "s":
return 0
elif last_term == "m":
return 1
last_index = readline(last_index, "Índice do último termo da progressão (n de an): ")
if last_index == "s":
return 0
elif last_index == "m":
return 1
# Saída:
print ("\nSn =", ((first_term + last_term) * last_index ) / 2)
# Menu de opções:
def main(ans):
while ans != "s":
print ("\nVocê deseja calcular: \n\n1) Todos os termos da progressão \n2) Um termo n da progressão (an) \n3) A razão da progressão (r) \n4) A soma geral dos termos da progressão (Sn) \n5) Sair\n")
ans = input()
while ans != "1" and ans != "2" and ans != "3" and ans != "4" and ans != "5" and ans != "s":
print ("\n*alternativa inválida*")
print ("Você deseja calcular: \n\n1) Todos os termos da progressão \n2) Um termo n da progressão (an) \n3) A razão da progressão (r) \n4) A soma geral dos termos da progressão (Sn) \n5) Sair\n")
ans = input()
if ans == "5" or ans == "s":
break
# Todos os termos da progressão:
if ans == "1":
print ("\n*Para sair, entre com 's'. Para voltar ao menu de opções, entre com 'm'* \nInforme-me:\n")
while ans == "1":
result = whole_sequence()
if result == 0:
ans = "s"
elif result == 1:
ans = None
# Termo n da progressão (an):
if ans == "2":
print ("\n*Para sair, entre com 's'. Para voltar ao menu de opções, entre com 'm'* \nInforme-me:\n")
while ans == "2":
result = find_term()
if result == 0:
ans = "s"
elif result == 1:
ans = None
# Razão da progressão:
if ans == "3":
print ("\n*Para sair, entre com 's'. Para voltar ao menu de opções, entre com 'm'*\nInforme-me dois termos consecutivos da progressão:\n")
while ans == "3":
result = find_ratio()
if result == 0:
ans = "s"
elif result == 1:
ans = None
# Soma geral dos termos (Sn):
if ans == "4":
print ("\n*Para sair, entre com 's'. Para voltar ao menu de opções, entre com 'm'* \nInforme-me:\n")
while ans == "4":
result = general_sum()
if result == 0:
ans = "s"
elif result == 1:
ans = None
main(None)
print ("\nAdeus!")