Problem about making my code stop the current function if it detects a non accepted value

Viewed 30

I’m currently trying to solve a problem in which you are given an Int number, that number dictates the amount of inputs you need. The goal is to determine the temperature in two zones of a barn. There are 3 thermometers in the room, so if for example the first input = 1 you need to ask for three letters (one for each thermometer) and 3 temperatures (one for each too). There are only 3 accepted value for the letters so, if a different one is entered the program should send an error code and not calculate the average temperature of the room. This is the code I have. I already solved everything, but I need to find a way in which if a wrong letter is typed, the program will just skip the calculation of those three values. Idk if you are going to understand my question, I’ve just started coding, so any help is highly appreciated. If you do not understand my question I will gladly try to explain it better. Thanks My code:

mediciones = int(input())
input()
numero_de_medicion = 0

def cambio(T1, porcentaje):
    Zona = Temp + Temp * (porcentaje)
    return Zona

def promedio(Z,T2):
    temp = (Z+T2)/2
    return temp

def sumar(n):
    numero = n+1
    return numero    

for termometro in range(mediciones):
    for medicion in range(3):
        Zona = input()
        Tempo = input()
        try:
            Temp = float(Tempo)
            esa = True
        except:
            print(f'invalid temperature {Tempo}')
            esa = False
        if Zona == 'E':
            TempE = cambio (Temp, 0.025)
        elif Zona == 'C':
            TempC = cambio(Temp, -0.15)
        elif Zona == 'O':
            TempO = Temp
        else:
            print('invalid sensor F')
            
    numero_de_medicion +=1
    
         
    if True:
        zona1= promedio(TempO,TempC)
        zona2= promedio(TempC,TempE)
        
        print(f'{numero_de_medicion}: {zona1:.2f} {zona2:.2f}')
1 Answers

If I am understanding properly, what you want to check if the zone is correct.

You should do this:

mediciones = int(input())
input()
numero_de_medicion = 0

def cambio(T1, porcentaje):
    Zona = Temp + Temp * (porcentaje)
    return Zona

def promedio(Z,T2):
    temp = (Z+T2)/2
    return temp

def sumar(n):
    numero = n+1
    return numero    

for termometro in range(mediciones):
    for medicion in range(3):
        def result():
            if True:
                zona1= promedio(TempO,TempC)
                zona2= promedio(TempC,TempE)
                print(f'{numero_de_medicion}: {zona1:.2f} {zona2:.2f}')

        Zona = input()
        Tempo = input()
        try:
            Temp = float(Tempo)
            esa = True
        except:
            print(f'invalid temperature {Tempo}')
            esa = False
        if Zona == 'E':
            TempE = cambio (Temp, 0.025)
            result()
        elif Zona == 'C':
            TempC = cambio(Temp, -0.15)
            result()
        elif Zona == 'O':
            TempO = Temp
            result()
        else:
            print('invalid sensor F')
            
    numero_de_medicion +=1

Is this what you want to do? Please let me know so I can find a better solution

Related