导入
条件语句
学习使用 if、elif 和 else 语句控制程序流程,根据特定条件执行代码。
1temperature = 35 2 3print("Checking safety...") 4 5if temperature > 30: 6 print("Warning: High temperature detected!") # To be executed, if True 7 print("Engaging cooling system.") # To be executed, if True 8 9print("System check complete.")
...
Flowchart of if and else statements
1password = "secret_code" 2input_attempt = "password123" 3 4if input_attempt == password: 5 print("Access Granted.") 6else: 7 print("Access Denied.")
1traffic_light = "Yellow" 2 3if traffic_light == "Red": 4 print("Stop immediately.") 5elif traffic_light == "Yellow": 6 print("Slow down and prepare to stop.") 7elif traffic_light == "Green": 8 print("Proceed with caution.") 9else: 10 print("Error: Unknown signal status.")
1has_ticket = True 2is_vip = False 3 4if has_ticket: 5 print("Welcome to the event.") 6 if is_vip: 7 print("Please proceed to the VIP lounge.") 8 else: 9 print("Your seat is in the general admission area.") 10else: 11 print("You must purchase a ticket to enter.")
...
Flowchart of nested conditional statements
No configuration found for ID: 885220ccd0a0eb04532a258ee6cfb47363f6bb9e8bdaf87ff82337d7b15ee577