python - 使用列表、函数和循环编写 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")
解决方案
# 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()
推荐阅读
- bixby - 如何显示 Bixby 确认视图?
- amazon-web-services - 如何将 aws 代理主机设置为 Spark 配置
- rust - 在 while 循环中更新可变 HashMap
- java - 您如何读取下载到浏览器中的 CSV 文件的内容?
- bash - 在(查找命令)时排除某个目录 - BASH
- java - 运行 JUnit/Mockito 测试时出现 org.springframework.http.converter.HttpMessageNotReadableException
- python - Angular 6 的全栈 Web 应用程序仅在 Chrome 中运行
- php - phpmailer和sendmail
- generics - 如何管理激增的、深度嵌套的类型参数?
- amazon-web-services - 使用带有 Amplify 的现有 AppSync API