【问题标题】:Write a Python program using lists, function and loops that will prompt a user to enter a temperature as an integer使用列表、函数和循环编写 Python 程序,提示用户以整数形式输入温度
【发布时间】:2020-06-11 16:43:12
【问题描述】:

使用会提示的列表、函数和循环编写 Python 程序 用户以整数形式输入温度。您的程序将打印“它 如果温度超过 100,则“很热”,如果温度超过 100,则“很冷” 60以下,如果温度在61到99之间,“正好” 包括的。该程序继续询问温度,并且 如上所述评估它们,直到用户输入温度 0(到 退出程序)。显示了预期程序的示例 下面:

Please enter a temperature: 95
It is just right.
Please enter a temperature: 110
It is hot.
Please enter a temperature: 32
It is cold.
Please enter a temperature: 0
Good bye! 
temp = int(input("Enter the temperature: ")) 
while temp != 1: 
    if temp >= 100: 
        print ("It is hot") 
    elif temp <= 60:
        print ("It is cold")
    elif temp == 0:
        print ("Good bye")
    else: 
        print ("It is just right")

【问题讨论】:

    标签: python


    【解决方案1】:

    三个建议:

    1. 将input 移动到while 循环内,以便在循环的每次迭代中提示输入。

    2. 在True而不是temp != 1上创建while循环,这样如果用户输入1,它就不会提前退出。

    3. 将break 语句添加到temp == 0 块中,以便在该点退出循环。

    【讨论】:

      【解决方案2】:
      # firsly define a function
      
      def solve():
      
          # define a infinite loop
          while True:
      
              # take int number as input 
              temp = int(input("Enter the temperature: ")) 
      
              if temp == 0:
                  print ("Good bye!")
                  # if temperature is zero exit the loop using "break" 
                  break
      
              # write other conditions
      
              if temp >= 100:
                  print ("It is hot.")
              elif temp <= 60:
                  print ("It is cold.")
              else :
                  print ("It is just right.")
      
      # call the function
      solve()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-29
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        • 2016-11-03
        • 1970-01-01
        • 2020-07-29
        相关资源
        最近更新 更多