【问题标题】:Problems with Simple Temperature Conversion Program Python3简单温度转换程序 Python3 的问题
【发布时间】:2020-11-11 10:47:38
【问题描述】:

我目前正在编写一个简单的温度转换程序,需要满足以下条件:

  1. 检查用户输入的是摄氏温度还是华氏温度
  2. 接受大写或小写的指定值(例如,c 或 C 代表摄氏温度)
  3. 如果用户未输入摄氏度或华氏度,则打印错误消息
  4. 转换为备用
  5. 打印值,指定是摄氏度还是华氏度

目前一切正常,除了打印出错误消息。例如,我只是输入 0 而没有温度约定。程序只显示: degree = int(temp[:-1]) #所有字符串,除了最后一个字符 ValueError: int() 以 10 为底的无效文字:''

我想做的是,即使我在提示输入时输入 0,它也只会在 else 语句中显示“输入正确约定”消息。

我正在使用的代码:

temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")
degree = int(temp[:-1]) #all of the string except for its last character
input_type = temp[-1] #get the last character

print("You entered: ", temp)
print("The degree entry is: ", degree)
print("The degree type is: ", input_type)


# Add code here
output_type = 0
result = 0
if input_type.upper() == "C":
  result = int(round((9 * degree) / 5 + 32))
  output_type = "F"
  print("The temperature in", output_type, "is", result, "degrees.")
elif input_type.upper() == "F":
  result = int(round((degree - 32) * 5 / 9))
  output_type = "C"
  print("The temperature in", output_type, "is", result, "degrees.")

else:
    print("Input proper convention.")
    

【问题讨论】:

  • 一个简单的解决方法是在 ifelif 块内计算 degree
  • 不要检查最后不是“C”或“F”,而是检查开头

标签: python python-3.x


【解决方案1】:

我没有能力对@bigbounty 的帖子发表评论。因此使用修改后的代码进行更新。您可以使用它来减少行数。

您可以直接使用 temp[-1] 检查 C 和 F。此外,如果您使用 .upper(),那么您将始终检查“C”和“F”。

另外,既然你已经检查了 C 或 F,你知道里面的条件要么是 C 要么是 F。所以你可以只使用 if 和 else。

temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")
if temp[-1].upper() in ('C','F'):
    degree = int(temp[:-1]) #all of the string except for its last character

    print("You entered: ", temp)
    print("The degree entry is: ", degree)
    print("The degree type is: ", temp[-1])

    if temp[-1].upper() == 'C':
        result = int(round((9 * degree) / 5 + 32))
        output_type = 'F'
    else: #will be 'F'
        result = int(round((degree - 32) * 5 / 9))
        output_type = 'C'
    print("The temperature in", output_type, "is", result, "degrees.")
else:
    print("Input proper convention.")

感谢您让我有机会回答您的问题。很有趣。

【讨论】:

    【解决方案2】:

    您可以像这样检查用户输入是否有效:

    def check_user_input(user_input):
      valid_units = ['c', 'C', 'F', 'f']
      for unit in valid_units:
          if unit in str(user_input) :
             return True    
      return False   
    

    或者干脆if str(user_input)[-1] not in {'c', 'C', 'f', 'F'}: do something

    【讨论】:

      【解决方案3】:

      将转换逻辑放在if 块内

      temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")
      
      if temp.endswith("C") or temp.endswith("F"):
          degree = int(temp[:-1]) #all of the string except for its last character
          input_type = temp[-1] #get the last character
          print("You entered: ", temp)
          print("The degree entry is: ", degree)
          print("The degree type is: ", input_type)
      
      
          # Add code here
          output_type = 0
      
          result = 0
      
      
          if input_type.upper() == "C":
              result = int(round((9 * degree) / 5 + 32))
              output_type = "F"
              print("The temperature in", output_type, "is", result, "degrees.")
          elif input_type.upper() == "F":
              result = int(round((degree - 32) * 5 / 9))
              output_type = "C"
              print("The temperature in", output_type, "is", result, "degrees.")
      
      else:
          print("Input proper convention.")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多