I have the following problem on my tuple

Asked

Viewed 52 times

0

def convertFarenheit(n):
    return (n * (9/5)) + 32

def convertCelsius(n):
    return (n - 32) * (5/9)

def graus(temp1,escala1,temp2,escala2):
    dados = ()
    if escala1 == 'C' and escala2 == 'C':
        soma = temp1 + temp2
        dados = (soma, 'C')

    elif escala1 == 'F' and escala2 == 'F':
        soma = temp1 + temp2
        dados = (soma, 'F')

    elif escala1 == 'F' and escala2 == 'C':
        soma = convertCelsius(temp1) + temp2
        dados = (soma, 'C')

    elif escala1 == 'C' and escala2 == 'F':
        soma = convertFarenheit(temp1) + temp2
        dados = (soma, 'F')

    return dados
def main(): 
    temp1 = float(input())
    escala1 = input().upper()[0]

    temp2 = float(input())
    escala2 = input().upper()[0]
    print(graus(temp1,escala1,temp2,escala2))

if __name__ == '__main__':
    main()

Examples of entries:

  • temp1 = -84.6
  • escala1 = F
  • temp2 = 15.7
  • scala2 = C

Return will be : (-49.077777777777, 'C').

Put fstring f{soma:.4f} to be able to round off to 4 decimal places will return : ('-49.0778', 'C'), but I do not want to return with the quotes in the float.

  • 1

    Welcome to Stack Overflow! Please explain the problem better as your question is not noticeable. See Help Center How to Ask.

  • I’ve already made the change

2 answers

1


If I understand correctly, you want to round up the number without turning it into a str, correct?

If this is the case, you should follow the following flow:

First, round up your temperature value. Then print it (not what you tried to do, where you did everything in the print). Always one step at a time.

And how round floats in Python? Well, one of the methods is the function round():

>>> temp = -49.07777777777777
>>> round(temp, 4)
-49.0778

0

From what I understand, you want to implement a code that is able to perform conversions of temperatures and print their values with 4 decimal places.

Well, when we are working with temperature conversions we must make it clear what will be:

  1. The input scale;
  2. The output scale.

Yeah, with this information we’ll build the code.

One of the right ways to perform temperature conversions is to:

def converte_fahrenheit():
    fahrenheit = (((temp * 9) / 5) + 32)
    return fahrenheit


def converte_celcius():
    celcius = ((5 * (temp - 32)) / 9)
    return celcius


# Capturando e tratando a escala de entrada
resp = input('Desejas digitar uma temperatura em °C ou °F? [C/F] ').upper()
while (len(resp) != 1) or (resp not in 'CF'):
    print('\033[31mValor INVÁLIDO! Digite apenas a letra "C" ou "F"!\033[m')
    resp = input('Desejas digitar uma temperatura em °C ou °F? [C/F] ').upper()

# Verificando qual conversão será realizada
if resp == 'C':
    temp = float(input('Digite a temperatura em °C: '))
    converte_fahrenheit()

    print(f'{converte_fahrenheit():.4f} °F')
else:
    temp = float(input('Digite a temperatura em °F: '))
    converte_celcius()

    print(f'{converte_celcius():.4f} °C')

Note that when we execute the respective code we receive the following message: Desejas digitar uma temperatura em °C ou °F? [C/F] . Right now we must press or key C or the key F.

If we press the key C, we receive the following message: Digite a temperatura em °C:. After we typed the temperature in °C, the code flow will be directed to the function converte_fahrenheit(), where the temperature will be converted to °F.

If we press the key F, we receive the following message: Digite a temperatura em °F:. After we typed the temperature in °F, the code flow will be directed to the function converte_celcius(), where the temperature will be converted to °C.

Once you have done the desired conversion, the value will be displayed with 4 decimal places.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.