RoiMinuit
RoiMinuit

Reputation: 414

Skipped Elif Statements

I wrote a program for temperature conversion. Everything works well except the conversion for kelvin to Celsius, kelvin to Fahrenheit and vice versa. I have tried a lot of different codes to see if it would be fixed, but in vain. Please advise. Below is the entire code. You will find area of issue in the second function named ToTemp(); the first couple of statements work well, both if you input a lower cap temperature abbreviation or upper cap (i.e 34F or 34f). But anything else fails to kick in.

'''
Python program to convert temperature from Celsius,
Fahrenheit or Kelvin. These next lines of code (loc) introduce
the program and ask user for temperature to be converted.
'''
import sys # to exit when user makes error.

print ("\n****Welcome to your own advanced temperature conversion application!****\n")
print ("^^^What would you like to convert?^^^")
print ("Type 'C' for Celsius, 'F' for Fahrenheit and 'K' for Kelvin \
 after entering digits. (Ex: '34C')\n")
user_temp1 = input('--> ')

# function to determine which temperature user wishes to convert from.
def FromTemp(user_temp):

    if user_temp1[-1] == 'C' or user_temp1[-1] == 'c':
        print ("You wish to convert from Celsius.")

    elif user_temp1[-1] == 'F' or user_temp1[-1] == 'f':
        print ("You wish to convert from Fahrenheit.")

    elif user_temp1[-1] == 'K' or user_temp1[-1] == 'k':
        print ("Ah! Yes, the elusive Kelvin!")

    else:
        print ("You must think me mad; I only allow C, F and K.")
        sys.exit() # exits when user makes mistake.

FromTemp(user_temp1)

# asks user for desired conversion rate.
print ("\n^^^What would you like to convert to? (Enter 'C', 'F' or 'K')^^^\n")
destin_temp = input("--> ")

# stripping the numbers from the temperature symbol.
real_temp = user_temp1[:- 1]
raw_temp = int(real_temp)

# function to make conversion from and to appropriate thermal unit.
def ToTemp(raw_temp):

    if destin_temp == 'C' or destin_temp == 'c' and user_temp1[-1] == 'F' or user_temp1[-1] == 'f':
        f_to_c = round(float((raw_temp - 32) * 5/9), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_c, "degrees Celsius.\n")
        print ("****Thank you for stopping by!****\n") #good

    elif destin_temp == 'F' or destin_temp == 'f' and user_temp1[-1] == 'C' or user_temp1[-1] == 'c':
        c_to_f = round(float(raw_temp * (9/5) + 32), 2)
        print (real_temp, "degrees Celsius is", c_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") #good

    elif destin_temp == 'C' or destin_temp == 'c' and user_temp1[-1] == 'K' or user_temp1[-1] == 'k':
        k_to_c = round(float(raw_temp - 273.15), 2)
        print (real_temp, "degrees Kelvin is", k_to_c, "degrees Celsius.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif destin_temp == 'K' or destin_temp == 'k' and user_temp1[-1] == 'C' or user_temp1[-1] == 'c':
        c_to_k = round(float(raw_temp + 273.15), 2)
        print (real_temp, "degrees Celsius is", c_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif destin_temp == 'F' or destin_temp == 'f' and user_temp1[-1] == 'K' or user_temp1[-1] == 'k':
        k_to_f = round(float((raw_temp - 273.15) * (9/5) + 32), 2)
        print (real_temp, "degrees Kelvin is", k_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif destin_temp == 'K' or destin_temp == 'k' and user_temp1[-1] == 'F' or user_temp1[-1] == 'f':
        f_to_k = round(float(raw_temp - 32 *(5/9) + 273.15), 2)
        print (real_temp, "degrees Fahrenheit is", k_to_f, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    else:
        print("You made a mistake somewhere. Try again later.")
        sys.exit()

ToTemp(raw_temp)

Upvotes: 1

Views: 131

Answers (3)

codrelphi
codrelphi

Reputation: 1065

You get an error when you convert Fahrenheit to Kelvin because in the last elif, you compute f_to_k but you print k_to_f. Your error come from this part of your code. Also, it is better to use parentheses to have a clear condition. Here is the final code.

'''
Python program to convert temperature from Celsius,
Fahrenheit or Kelvin. These next lines of code (loc) introduce
the program and ask user for temperature to be converted.
'''
import sys # to exit when user makes error.

print ("\n****Welcome to your own advanced temperature conversion application!****\n")
print ("^^^What would you like to convert?^^^")
print ("Type 'C' for Celsius, 'F' for Fahrenheit and 'K' for Kelvin \
 after entering digits. (Ex: '34C')\n")
user_temp1 = input('--> ')

# function to determine which temperature user wishes to convert from.
def FromTemp(user_temp):

    if user_temp1[-1] == 'C' or user_temp1[-1] == 'c':
        print ("You wish to convert from Celsius.")

    elif user_temp1[-1] == 'F' or user_temp1[-1] == 'f':
        print ("You wish to convert from Fahrenheit.")

    elif user_temp1[-1] == 'K' or user_temp1[-1] == 'k':
        print ("Ah! Yes, the elusive Kelvin!")

    else:
        print ("You must think me mad; I only allow C, F and K.")
        sys.exit() # exits when user makes mistake.

FromTemp(user_temp1)

# asks user for desired conversion rate.
print ("\n^^^What would you like to convert to? (Enter 'C', 'F' or 'K')^^^\n")
destin_temp = input("--> ")

# stripping the numbers from the temperature symbol.
real_temp = user_temp1[:- 1]
raw_temp = int(real_temp)

# function to make conversion from and to appropriate thermal unit.
def ToTemp(raw_temp):

    if (destin_temp == 'C' or destin_temp == 'c') and (user_temp1[-1] == 'F' or user_temp1[-1] == 'f'):
        f_to_c = round(float((raw_temp - 32) * 5/9), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_c, "degrees Celsius.\n")
        print ("****Thank you for stopping by!****\n") #good

    elif (destin_temp == 'F' or destin_temp == 'f') and (user_temp1[-1] == 'C' or user_temp1[-1] == 'c'):
        c_to_f = round(float(raw_temp * (9/5) + 32), 2)
        print (real_temp, "degrees Celsius is", c_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") #good

    elif (destin_temp == 'C' or destin_temp == 'c') and (user_temp1[-1] == 'K' or user_temp1[-1] == 'k'):
        k_to_c = round(float(raw_temp - 273.15), 2)
        print (real_temp, "degrees Kelvin is", k_to_c, "degrees Celsius.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif (destin_temp == 'K' or destin_temp == 'k') and (user_temp1[-1] == 'C' or user_temp1[-1] == 'c'):
        c_to_k = round(float(raw_temp + 273.15), 2)
        print (real_temp, "degrees Celsius is", c_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif (destin_temp == 'F' or destin_temp == 'f') and (user_temp1[-1] == 'K' or user_temp1[-1] == 'k'):
        k_to_f = round(float((raw_temp - 273.15) * (9/5) + 32), 2)
        print (real_temp, "degrees Kelvin is", k_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif (destin_temp == 'K' or destin_temp == 'k') and (user_temp1[-1] == 'F' or user_temp1[-1] == 'f'):
        f_to_k = round(float(raw_temp - 32 *(5/9) + 273.15), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    else:
        print("You made a mistake somewhere. Try again later.")
        sys.exit()

ToTemp(raw_temp)

Update: Here is a better organized code.

'''
Python program to convert temperature from Celsius,
Fahrenheit or Kelvin. These next lines of code (loc) introduce
the program and ask user for temperature to be converted.
'''
import sys # to exit when user makes error.


# function to determine which temperature user wishes to convert from.
def FromTemp(user_temp):
    if user_temp1[-1] == 'C' or user_temp1[-1] == 'c':
        print ("You wish to convert from Celsius.")
    elif user_temp1[-1] == 'F' or user_temp1[-1] == 'f':
        print ("You wish to convert from Fahrenheit.")
    elif user_temp1[-1] == 'K' or user_temp1[-1] == 'k':
        print ("Ah! Yes, the elusive Kelvin!")
    else:
        print ("You must think me mad; I only allow C, F and K.")
        sys.exit() # exits when user makes mistake.

# function to make conversion from and to appropriate thermal unit.
def ToTemp(raw_temp):
    if destin_temp.lower() == 'c' and user_temp1[-1].lower() == 'f':
        f_to_c = round(float((raw_temp - 32) * 5/9), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_c, "degrees Celsius.\n")
        print ("****Thank you for stopping by!****\n") #good
    elif destin_temp.lower() == 'f' and user_temp1[-1].lower() == 'c':
        c_to_f = round(float(raw_temp * (9/5) + 32), 2)
        print (real_temp, "degrees Celsius is", c_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") #good
    elif destin_temp.lower() == 'c' and user_temp1[-1].lower() == 'k':
        k_to_c = round(float(raw_temp - 273.15), 2)
        print (real_temp, "degrees Kelvin is", k_to_c, "degrees Celsius.")
        print ("****Thank you for stopping by!****\n") # doesn't work
    elif destin_temp.lower() == 'k' and user_temp1[-1].lower() == 'c':
        c_to_k = round(float(raw_temp + 273.15), 2)
        print (real_temp, "degrees Celsius is", c_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work
    elif destin_temp.lower() == 'f' and user_temp1[-1].lower() == 'k':
        k_to_f = round(float((raw_temp - 273.15) * (9/5) + 32), 2)
        print (real_temp, "degrees Kelvin is", k_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") # doesn't work
    elif destin_temp.lower() == 'k' and user_temp1[-1].lower() == 'f':
        f_to_k = round(float(raw_temp - 32 *(5/9) + 273.15), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work
    else:
        print("You made a mistake somewhere. Try again later.")
        sys.exit()


if __name__ == "__main__":

    print ("\n****Welcome to your own advanced temperature conversion application!****\n")
    print ("^^^What would you like to convert?^^^")
    print ("Type 'C' for Celsius, 'F' for Fahrenheit and 'K' for Kelvin \
     after entering digits. (Ex: '34C')\n")
    user_temp1 = input('--> ')

    FromTemp(user_temp1)

    # asks user for desired conversion rate.
    print ("\n^^^What would you like to convert to? (Enter 'C', 'F' or 'K')^^^\n")
    destin_temp = input("--> ")

    # stripping the numbers from the temperature symbol.
    real_temp = user_temp1[:- 1]
    raw_temp = int(real_temp)

    ToTemp(raw_temp)

Upvotes: 2

Michael Green
Michael Green

Reputation: 810

Python isn't interpreting your and/or statements the way you think it is.

You're thinking of it as if (1 or 2) and (3 or 4):

But python isn't seeing it like that. It's reading it 1 or (2 and 3) or 4:

clearing up the code by condensing it down with an .upper() command fixes your issue.

'''
Python program to convert temperature from Celsius,
Fahrenheit or Kelvin. These next lines of code (loc) introduce
the program and ask user for temperature to be converted.
'''
import sys # to exit when user makes error.

print ("\n****Welcome to your own advanced temperature conversion application!****\n")
print ("^^^What would you like to convert?^^^")
print ("Type 'C' for Celsius, 'F' for Fahrenheit and 'K' for Kelvin \
 after entering digits. (Ex: '34C')\n")
user_temp1 = input('--> ')

# function to determine which temperature user wishes to convert from.
def FromTemp(user_temp):

    if user_temp1[-1].upper() == 'C':
        print ("You wish to convert from Celsius.")

    elif user_temp1[-1].upper() == 'F':
        print ("You wish to convert from Fahrenheit.")

    elif user_temp1[-1].upper() == 'K':
        print ("Ah! Yes, the elusive Kelvin!")

    else:
        print ("You must think me mad; I only allow C, F and K.")
        sys.exit() # exits when user makes mistake.

FromTemp(user_temp1)

# asks user for desired conversion rate.
print ("\n^^^What would you like to convert to? (Enter 'C', 'F' or 'K')^^^\n")
destin_temp = input("--> ")

# stripping the numbers from the temperature symbol.
real_temp = user_temp1[:- 1]
raw_temp = int(real_temp)

# function to make conversion from and to appropriate thermal unit.
def ToTemp(raw_temp):

    if destin_temp.upper() == 'C' and user_temp1[-1].upper() == 'F':
        f_to_c = round(float((raw_temp - 32) * 5/9), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_c, "degrees Celsius.\n")
        print ("****Thank you for stopping by!****\n") #good

    elif destin_temp.upper() == 'F' and user_temp1[-1].upper() == 'C':
        c_to_f = round(float(raw_temp * (9/5) + 32), 2)
        print (real_temp, "degrees Celsius is", c_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") #good

    elif destin_temp.upper() == 'C' and user_temp1[-1].upper() == 'K':
        k_to_c = round(float(raw_temp - 273.15), 2)
        print (real_temp, "degrees Kelvin is", k_to_c, "degrees Celsius.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif destin_temp.upper() == 'K' and user_temp1[-1].upper() == 'C':
        c_to_k = round(float(raw_temp + 273.15), 2)
        print (real_temp, "degrees Celsius is", c_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif destin_temp.upper() == 'F' and user_temp1[-1].upper() == 'K':
        k_to_f = round(float((raw_temp - 273.15) * (9/5) + 32), 2)
        print (real_temp, "degrees Kelvin is", k_to_f, "degrees Fahrenheit.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    elif destin_temp.upper() == 'K' and user_temp1[-1].upper() == 'F':
        f_to_k = round(float(raw_temp - 32 *(5/9) + 273.15), 2)
        print (real_temp, "degrees Fahrenheit is", f_to_k, "degrees Kelvin.")
        print ("****Thank you for stopping by!****\n") # doesn't work

    else:
        print("You made a mistake somewhere. Try again later.")
        sys.exit()

Upvotes: 1

lenik
lenik

Reputation: 23536

I'd rather rewrite this:

if destin_temp == 'C' or destin_temp == 'c' and user_temp1[-1] == 'F' or user_temp1[-1] == 'f':

as this:

destin_temp = destin_temp.lower()
user_temp = user_temp1[-1].lower()
if destin_temp == 'c' and user_temp == 'f':

to convert everything to the lowercase (or uppercase) and deal with a much simpler expressions.

Upvotes: 3

Related Questions