Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Exception is always raised + my function is not working

Kaworu

Active Coder
Hi.

I have made a really simple Python program that calculates values in Celcius, Fahrenheit and Kelvin degrees. Sadly, I have two problems with it:

1. There is a check if the value of degrees (string, C or K or F) is correct (if not, then there is an exception being raised). Sadly, the code is always working, even when for instance you are entering "C". As such, every instance of the program running brings the exception, which makes it impossible to use it.

2. I have a function ( def print_results() ) that should print nicely the values to prompt, but it seems to not be working at all. This also makes the program unworkable.

If somebody could help me, I would be grateful 🙂

CODE below:

Python:
################################################
#          FUNCTIONS AND GLOBAL VARIABLES
################################################

return_string = None

def CtoK(celcius):
    kelvin = 273.15 + celcius
    return kelvin

def CtoF(celcius):
    fahrenheit = 32 + ( 9 / 5 * celcius)
    return fahrenheit

def KtoC(kelvin):
    celcius = kelvin - 273.15
    return celcius

def KtoF(kelvin):
    fahrenheit = 1.8 * (kelvin - 273) + 32
    return fahrenheit

def FtoC(fahrenheit):
    celcius = (fahrenheit - 32) * 5 / 9
    return celcius

def FtoK(fahrenheit):
    kelvin = (fahrenheit + 459.67) * 5 / 9
    return kelvin

def get_return_string(type_of_degrees):

    global return_string

    if type_of_degrees == "C":
        return_string = "C"
    elif type_of_degrees == "F":
        return_string = "F"
    else:
        return_string = "K"

'''
def set_temperature(temperature_value):

    global celcius
    global fahrenheit
    global kelvin

    if return_string == "C":
        celcius = temperature_value
    elif return_string == "F":
        fahrenheit = temperature_value
    else:
        kelvin = temperature_value
'''

def print_results():
    if return_string.upper() == "C":
        print(temperature_value, "Celcius is", CtoK(temperature_value), "Kelvin")
        print(temperature_value, "Celcius is", CtoF(temperature_value), "Fahrenheit")
    elif return_string.upper() == "F":
        print(temperature_value, "Fahrenheit is", FtoC(temperature_value), "Celcius")
        print(temperature_value, "Fahrenheit is", FtoK(temperature_value), "Kelvin")
    elif return_string.upper() == "K":
        print(temperature_value, "Kelvin is", KtoC(temperature_value), "Celcius")
        print(temperature_value, "Kelvin is", KtoF(temperature_value), "Kelvin")

################################################
#          PROGRAM
################################################

print("Welcome to the temperature calculator! Enter the temperature below to find its value in different degrees!")

type_of_degrees = str(input("What kind of degrees you wanna recalculate? [C/F/K] "))

if type_of_degrees.upper() != "C" or "F" or "K": # Should be working only if type_of_degrees is different than C or K or F...
    raise Exception("Wrong value! Should be C or F or K!") #... but works all the time.

temperature_value = int(input("Enter the number of degrees "))

get_return_string(type_of_degrees)
print_results()
 
You have made a simple mistake. In if statements, statements seperated by and / or are completely separate. This means, instead of doing `type_of_degrees.upper() != "C" or "F" or "K":` you should do `type_of_degrees.upper() != "C" and type_of_degrees.upper() != "F" and type_of_degrees.upper() != "K"`.
Python:
################################################
#          FUNCTIONS AND GLOBAL VARIABLES
################################################

return_string = None

def CtoK(celcius):
    kelvin = 273.15 + celcius
    return kelvin

def CtoF(celcius):
    fahrenheit = 32 + ( 9 / 5 * celcius)
    return fahrenheit

def KtoC(kelvin):
    celcius = kelvin - 273.15
    return celcius

def KtoF(kelvin):
    fahrenheit = 1.8 * (kelvin - 273) + 32
    return fahrenheit

def FtoC(fahrenheit):
    celcius = (fahrenheit - 32) * 5 / 9
    return celcius

def FtoK(fahrenheit):
    kelvin = (fahrenheit + 459.67) * 5 / 9
    return kelvin

def get_return_string(type_of_degrees):

    global return_string

    if type_of_degrees == "C":
        return_string = "C"
    elif type_of_degrees == "F":
        return_string = "F"
    else:
        return_string = "K"

'''
def set_temperature(temperature_value):

    global celcius
    global fahrenheit
    global kelvin

    if return_string == "C":
        celcius = temperature_value
    elif return_string == "F":
        fahrenheit = temperature_value
    else:
        kelvin = temperature_value
'''

def print_results():
    if return_string.upper() == "C":
        print(temperature_value, "Celcius is", CtoK(temperature_value), "Kelvin")
        print(temperature_value, "Celcius is", CtoF(temperature_value), "Fahrenheit")
    elif return_string.upper() == "F":
        print(temperature_value, "Fahrenheit is", FtoC(temperature_value), "Celcius")
        print(temperature_value, "Fahrenheit is", FtoK(temperature_value), "Kelvin")
    elif return_string.upper() == "K":
        print(temperature_value, "Kelvin is", KtoC(temperature_value), "Celcius")
        print(temperature_value, "Kelvin is", KtoF(temperature_value), "Kelvin")

################################################
#          PROGRAM
################################################

print("Welcome to the temperature calculator! Enter the temperature below to find its value in different degrees!")

type_of_degrees = str(input("What kind of degrees you wanna recalculate? [C/F/K] "))

if type_of_degrees.upper() != "C" and type_of_degrees.upper() != "F" and type_of_degrees.upper() != "K": # Should be working only if type_of_degrees is different than C or K or F...
    raise Exception("Wrong value! Should be C or F or K!") #... but works all the time.

temperature_value = int(input("Enter the number of degrees "))

get_return_string(type_of_degrees)
print_results()
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom