- 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.
- 數(shù)據(jù)展現(xiàn)的藝術(shù)
- 智能傳感器技術(shù)與應(yīng)用
- 機器學習及應(yīng)用(在線實驗+在線自測)
- 分布式多媒體計算機系統(tǒng)
- 系統(tǒng)安裝與重裝
- 工業(yè)機器人應(yīng)用案例集錦
- 網(wǎng)站入侵與腳本攻防修煉
- 單片機技術(shù)項目化原理與實訓
- Artificial Intelligence By Example
- 寒江獨釣:Windows內(nèi)核安全編程
- WPF專業(yè)編程指南
- 機器人剛?cè)狁詈蟿恿W
- Mastering MongoDB 4.x
- 教育創(chuàng)新與創(chuàng)新人才:信息技術(shù)人才培養(yǎng)改革之路(四)
- 玩轉(zhuǎn)PowerPoint