- Python Programming with Raspberry Pi
- Sai Yamanoor Srihari Yamanoor
- 235字
- 2021-07-02 23:48:47
if-elif-else statement
In the preceding example, the program executes any piece of code under the else block for any user input other than yes that is if the user pressed the return key without providing any input or provided random characters instead of no, the if-elif-else statement works as follows:
check_address = input("Is your address correct(yes/no)? ")
if check_address == "yes":
print("Thanks. Your address has been saved")
elif check_address == "no":
del(address)
print("Your address has been deleted. Try again")
else:
print("Invalid input. Try again")
If the user input is yes, the indented code block under the if statement is executed. If the user input is no, the indented code block under elif (else-if) is executed. If the user input is something else, the program prints the message: Invalid input. Try again.
It is important to note that the code block indentation determines the block of code that needs to be executed when a specific condition is met. We recommend modifying the indentation of the conditional statement block and find out what happens to the program execution. This will help understand the importance of indentation in Python.
In the three examples that we discussed so far, it could be noted that an if statement does not need to be complemented by an else statement. The else and elif statements need to have a preceding if statement or the program execution would result in an error.
- 21天學通JavaScript
- Mastering VMware vSphere 6.5
- Hands-On Neural Networks with Keras
- Android游戲開發案例與關鍵技術
- 大數據時代
- Machine Learning with Apache Spark Quick Start Guide
- Photoshop行業應用基礎
- 工業機器人力覺視覺控制高級應用
- Spark大數據商業實戰三部曲:內核解密|商業案例|性能調優
- Xilinx FPGA高級設計及應用
- 筆記本電腦維修之電路分析基礎
- Visual Basic項目開發案例精粹
- Effective Business Intelligence with QuickSight
- 渲染王3ds Max三維特效動畫技術
- Hands-On Agile Software Development with JIRA