-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2
52 lines (47 loc) · 1.28 KB
/
lab2
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
import json
import requests
site="https://api.npoint.io/2b57052af2060e84dc86"
# Write the functions convert_number and replace_number here
# Follow the logic below.
# Trying to load JSON into text
r = requests.get(site)
print(r.json())
text = r.json()['users']
# print(text)
# Debug
for i in text:
print("parse " + str(i))
# call the function convert_number
# convert all elements (except the first one) into number and return it as a list
def convert_number(lst):
result = []
for i in lst:
try:
result.append(int(i))
except ValueError:
continue
return result
# a = ["oi","1"]
# b = ["3"]
# print(convert_number(a))
y = convert_number(text[0])
print("y")
print(y)
# call the function replace_number
# replace all number 1 by the number 10 in the function
def replace_number(number_list,being_replace,to_replace):
replaced_number_list=[]
for i in number_list:
if i==being_replace:
replaced_number_list.append(to_replace)
else:
replaced_number_list.append(i)
return replaced_number_list
z = replace_number(number_list = y, being_replace = 1, to_replace = 10)
print("z")
print(z)
sum = 0
for i in z:
sum = sum + i
print("sum = " + str(sum) + "; i =" + str(i))
print ("Total = " + str(sum))