为什么这个 python 代码块向我显示“这不是一个有效的数字”结果,我该如何解决它?[复制]

Why does this block of python code show me the "This is not a valid number" result and how do I fix it? [duplicate]

提问人:Pythony2345 提问时间:11/13/2023 最后编辑:Pythony2345 更新时间:11/13/2023 访问量:91

问:

这是一个在两个温度单位之间转换的程序。

while True:
    temp_mode = input("""(1)Conversion from Celsius to Kelvin
                (2)Conversion from Celsius to Fahrenheit
                (3)Conversion from Kelvin to Celsius
                (4)Conversion from Kelvin to Fahrenheit
                (5)Conversion from Fahrenheit to Celsius
                (6)Conversion from Fahrenheit to Kelvin """)


    if temp_mode == "1":

        temp1 = "Celsius"
        temp2 = "Kelvin"

        break
        
    elif temp_mode == "2":

        temp1 = "Celsius"
        temp2 = "Fahrenheit"

        break

    elif temp_mode == "3":

        temp1 = "Kelvin"
        temp2 = "Celsius"

        break

    elif temp_mode == "4":

        temp1 = "Kelvin"
        temp2 = "Fahrenheit"

        break

    elif temp_mode == "5":

        temp1 = "Fahrenheit"
        temp2 = "Celsius"

        break

    elif temp_mode == "6":

        temp1 = "Fahrenheit"
        temp2 = "Kelvin" 

        break

    else:

        print("This is not a valid number. Valid numbers are: 1, 2, 3, 4, 5 , 6.")

        continue


while True:
            print("Please separate decimals with a '.'.")

            temp1_value = input(temp1, ":")

            temp1_value_float = float(temp1_value)


            if temp1_value != temp1_value_float:
                print("This number is not a valid Number.")

                continue

            temp1_value = float(temp1_value)

            break

if temp_mode == "1":

        temp2_value = temp1_value + 273.15
        
elif temp_mode == "2":

    temp2_value = temp1_value * 9/5 + 32

elif temp_mode == "3":

    temp2_value = temp1_value - 273.15

elif temp_mode == "4":

    temp2_value = (temp1_value - 275.15) * 9/5 + 32 

elif temp_mode == "5":

    temp2_value = (temp1_value - 32) * 5/9

elif temp_mode == "6":

    temp2_value = (temp1_value - 32) * 5/9 + 273.15  

        
print(temp1_value, "degree", temp1, "are", temp2_value, "degree", temp2)

输出:

(1)Conversion from Celsius to Kelvin
                (2)Conversion from Celsius to Fahrenheit
                (3)Conversion from Kelvin to Celsius
                (4)Conversion from Kelvin to Fahrenheit
                (5)Conversion from Fahrenheit to Celsius
                (6)Conversion from Fahrenheit to Kelvin 2
This is not a valid number. Valid numbers are: 1, 2, 3, 4, 5 , 6.
(1)Conversion from Celsius to Kelvin
                (2)Conversion from Celsius to Fahrenheit
                (3)Conversion from Kelvin to Celsius
                (4)Conversion from Kelvin to Fahrenheit
                (5)Conversion from Fahrenheit to Celsius
                (6)Conversion from Fahrenheit to Kelvin

我在第一个输出后输入了“2”,我不明白为什么它说“这不是一个有效的数字。有效数字为:1、2、3、4、5、6。

请用“.”分隔小数点。 摄氏度:

我包含这个函数是因为我不想在有拼写错误或写错数字时出现语法错误。

那么我做错了什么,我该如何解决呢?如果有人对代码的改进提出建议,我愿意接受想法。

谢谢

蟒蛇 python-2.7

评论


答: 暂无答案