Tôi đang tạo một chương trình trên trăn cần giữ một số thông tin trên danh sách và chúng thực thi các phép toán trên chúng. Đây là mẫu mã của tôi:Làm cách nào để vận hành trên các thành phần trong danh sách?
VCentral = []
Atlantico=[]
Pacifico=[]
Norte=[]
Sur=[]
LVC=0
LA=0
LP=0
LN=0
LS=0
LTotal=0
def RegTemp(regcode):
global LVC
global LA
global LP
global LN
global LS
global LTotal
registro=[]
temp = int(input("Digite la temperatura: "))
fecha=input("Digite la fecha: ")
registro.extend((temp,fecha))
if regcode==1:
VCentral.extend(registro)
LVC+=1
LTotal+=1
if regcode==2:
Atlantico.extend(registro)
LA+=1
LTotal+=1
if regcode==3:
Pacifico.extend(registro)
LP+=1
LTotal+=1
if regcode==4:
Norte.extend(registro)
LN+=1
LTotal+=1
if regcode==5:
Sur.extend(registro)
LS+=1
LTotal+=1
Và sau đó tôi cần so sánh giá trị của nó với một giá trị khác. đây là một mẫu khác của hàm tôi đang cố gắng triển khai:
def Mayor(regcode):
if regcode==1:
may=0
for i in VCentral:
if i[0]>may:
may=i[0]
return may
if regcode==2:
may=0
for i in Atlantico:
if i[0]>may:
may=i[0]
return may
if regcode==3:
may=0
for i in Pacifico:
if i[0]>may:
may=i[0]
return may
if regcode==4:
may=0
for i in Norte:
if i[0]>may:
may=i[0]
return may
if regcode==5:
may=0
for i in Sur:
if i[0]>may:
may=i[0]
return may
Nếu bạn có thể cho tôi biết tại sao nó lại gây lỗi cho tôi, tôi sẽ đánh giá cao nó.
EDIT:
Traceback (most recent call last):
File "D:/tarea2.py", line 212, in <module>
Menu()
File "D:/tarea2.py", line 199, in Menu
print(EstadisticaZona(regcode))
File "D:/tarea2.py", line 165, in EstadisticaZona
print("Temperatura mayor: ",Mayor(2))
File "D:/tarea2.py", line 102, in Mayor
if i[0]>may:
TypeError: 'int' object is not subscriptable
lỗi bằng văn bản giúp bạn có thể nhận được câu trả lời hay hơn – DreadfulWeather
Lỗi gì sẽ xảy ra? Bạn nên sử dụng elif nếu bạn có nhiều nếu. – GiannisIordanou
Tôi đã thêm lỗi –