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

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.

主站蜘蛛池模板: 吴桥县| 成安县| 象州县| 定陶县| 乌鲁木齐县| 阿图什市| 扎赉特旗| 云霄县| 日照市| 东丽区| 嘉兴市| 沁源县| 德钦县| 鸡泽县| 三穗县| 时尚| 军事| 云浮市| 阜新市| 德化县| 桑植县| 林西县| 灵川县| 乌鲁木齐市| 鄱阳县| 平江县| 松滋市| 正蓝旗| 香格里拉县| 秭归县| 延川县| 云南省| 天水市| 光山县| 茌平县| 武邑县| 沙田区| 卢龙县| 武强县| 化德县| 虹口区|