跳过的Elif语句

时间:2019-11-12 01:35:56

标签: python if-statement

我写了一个温度转换程序。除了将开尔文转换为摄氏温度,将开尔文转换为华氏度,反之亦然之外,其他所有方法都运行良好。我尝试了很多不同的代码来查看它是否可以解决,但徒劳无功。请指教。以下是整个代码。您将在名为ToTemp()的第二个函数中找到问题所在。如果您输入的是上限温度下限的缩写或上限(即34F或34f),则前两句话效果很好。但是其他任何事情都无法解决。

'''
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)

3 个答案:

答案 0 :(得分:3)

我宁愿改写这个:

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

是这样的:

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

将所有内容转换为小写(或大写)并处理更简单的表达式。

答案 1 :(得分:2)

将华氏温度转换为开氏温度时会出错,因为在最后一个elif中,您计算​​了f_to_k,但打印了k_to_f。您的错误来自代码的这一部分。另外,最好使用括号来明确条件。这是最终代码。

'''
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)
  

更新:这是井井有条的代码。

'''
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)

答案 2 :(得分:1)

Python不会以您认为的方式解释您和/或陈述。

您正在考虑将其视为(1或2)和(3或4):

但是python并没有那样的感觉。它正在读取1或(2和3)或4:

通过使用.upper()命令压缩代码来清除代码,即可解决您的问题。

'''
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()