资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python中如何使用if语句

今天就跟大家聊聊有关python中如何使用if语句,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了望奎免费建站欢迎大家使用!

if语句:判断语句,根据条件判断是否继续执行

输入:

#!/usr/bin/python 

# Filename: if.py

number = 23 

guess = int(input('Enter an integer : '))

if guess == number:    

    print('Congratulations, you guessed it.') 

# New block starts here    

    print('(but you do not win any prizes!)') 

# New block ends here 

elif guess < number:    

    print('No, it is a little higher than that') 

# Another block    

# You can do whatever you want in a block ... 

else:    

print('No, it is a little lower than that')    

# you must have guess > number to reach here

print('Done') 

# This last statement is always executed, after the if statement is executed 

第一遍运行输出:

 $ python if.py

Enter an integer : 50    

No, it is a little lower than that    

Done 

第二遍运行输出:

 $ python if.py    

Enter an integer : 22

No, it is a little higher than that    

Done

第三遍运行输出:

 $ python if.py    

Enter an integer : 23    

Congratulations, you guessed it.    (but you do not win any prizes!)    

Done 

解释:

上面的执行代码时一个猜数字的小游戏,运行程序后,提示输入一个数字,用户通过在控制台输入数字来执行猜数字。

if语句在这里起判断作用,如果条件满足(即猜的数字相等)就提示猜对。

如果条件没满足会执行if条件的分支语句elif,

2级判断:猜的数字太小了

如果条件仍然没满足,继续会执行if条件的分支语句elif,3级判断:猜的数字太大了

所有可判断的条件执行完毕,输出结果。

输入一个数字进行if条件判断,只会有1种输出:要么是相等提示,猜对;要么是大了;要么是小了。

输入:

if True:    

print('Yes, it is true') 

输出:

Yes, it is true

解释:

if语句结构

if 条件 :

    print(“内容”)

注意上面的冒号,以及冒号后的语句缩进。

看完上述内容,你们对python中如何使用if语句有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前名称:python中如何使用if语句
文章网址:http://cdkjz.cn/article/ishhdo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220