小空调程序代码(Python版):
```python
# 假设空调的温度范围为16~30度
# 用户可以通过输入指令控制空调的开关和温度
# 初始化空调状态
is_on = False
temperature = 25
def turn_on():
global is_on
if not is_on:
is_on = True
print("空调已开启")
else:
print("空调已经开启了")
def turn_off():
global is_on
if is_on:
is_on = False
print("空调已关闭")
else:
print("空调已经关闭了")
def set_temperature(new_temperature):
global temperature
if is_on:
if 16 <= new_temperature <= 30:
temperature = new_temperature
print(f"空调温度已设置为{temperature}度")
else:
print("温度超出范围,设置失败")
else:
print("请先开启空调")
def get_status():
if is_on:
print(f"空调状态:开启,当前温度:{temperature}度")
else:
print("空调状态:关闭")
while True:
print("\n请输入指令:")
print("1. 开启空调")
print("2. 关闭空调")
print("3. 设置温度")
print("4. 查看空调状态")
print("5. 退出程序")
choice = input()
if choice == "1":
turn_on()
elif choice == "2":
turn_off()
elif choice == "3":
new_temperature = int(input("请输入温度:"))
set_temperature(new_temperature)
elif choice == "4":
get_status()
elif choice == "5":
print("程序已退出")
break
else:
print("无效指令,请重新输入")
这段代码实现了一个简单的空调控制程序。用户可以通过输入指令选择开启空调、关闭空调、设置温度或查看空调状态。程序使用全局变量保存空调状态和温度,通过函数进行状态切换和温度设置。程序会在控制台打印相关操作的结果。用户可以通过输入"5"来退出程序。
书辞网,综合资讯门户 备案号:湘ICP备15010808号-3
Copyright © 2016-2023 SHUCI.NET All Rights Reserved