官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 鸡东县| 嘉善县| 库车县| 四子王旗| 义乌市| 河南省| 西藏| 麻栗坡县| 宁强县| 靖西县| 崇仁县| 孝义市| 丹凤县| 平舆县| 延寿县| 定日县| 云和县| 运城市| 信宜市| 扶余县| 丽江市| 英吉沙县| 安岳县| 临漳县| 织金县| 乌拉特后旗| 左权县| 顺昌县| 腾冲县| 会理县| 城步| 潞城市| 灌南县| 涟源市| 南充市| 乐山市| 文安县| 缙云县| 句容市| 襄城县| 琼海市|