首页 > 解决方案 > 使用列表、函数和循环编写 Python 程序,提示用户以整数形式输入温度

问题描述

使用列表、函数和循环编写 Python 程序,提示用户以整数形式输入温度。如果温度超过 100,您的程序将打印“it is hot”,如果温度低于 60,则打印“it is cold”,如果温度在 61 到 99(含)之间,您的程序将打印“it is just right”。程序继续询问温度,并如上所述评估它们,直到用户输入温度 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

解决方案


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

推荐阅读