4. The conditional statement acquaintance Python

Ergou one day go to the fair, his girlfriend told him to buy a watermelon it, if the tomatoes, then buy two?

Normally Ergou brought back should be a watermelon or a watermelon two tomatoes. But it is Ergou programs ape, the last two dogs back with two watermelons

Why is this? We first take a look at these words in mind there are two dogs how to run.

The diamond is a conditional statement that we have to introduce?

What is a conditional sentence?

Conditional statement is used to determine the impact of the program to make the statement, his main field has

if

if ... else

if ... elif ... else

Let's look at a few examples:

There are a good variety of fruits on set

fruit = ['a','b','c','d','西红柿']

if '西红柿' in fruit:
    # python 的if没有大括号,需要另起一行,四个空格
    print('买两个西瓜')
else:
    print('买一个西瓜')

It is clear that such a large market is definitely a tomato, so output

买两个西瓜
Development: determine a list of objects which are not in use

Now I just do not want to judge with tomatoes

Ergou girlfriend upgraded, if there is to buy two tomatoes tomatoes; if there are no tomatoes apples to buy two apples; if not, then buy a watermelon

fruit = ['a','b','c','d','西红柿']

if '西红柿' in fruit:
    # python 的if没有大括号,需要另起一行,四个空格
    print('买两个西红柿')
elif '苹果' in fruit:
    print('买两个苹果')
else:
    print('买一个西瓜')

Of course women are fickle thing, girlfriend demands continue to escalate, it would continue to increase

Analyzing conditions like elif

If you want to buy more than that girlfriend of it?

If there are tomatoes and apples, then buy a tomato two apples; one, then it would have to buy a watermelon, two are not, then buy a pound of strawberries.

This is much more complicated

The following conditions are related to query multiple connector or a non-or / and /!

fruit = ['a','b','c','d','西红柿']

if '西红柿' in fruit and '苹果' in fruit :
    # python 的if没有大括号,需要另起一行,四个空格
    print('买一个西红柿两个苹果')
elif '西红柿' in fruit or '苹果' in fruit:
    print('买一个西瓜')
else:
    print('买一斤草莓')
expand:
== 等于
> 大于
< 小于
>= 大于等于
<=小于等于
!= 不等于

The following program will cover a majority judgment condition

num = 9
# 判断值是否在0~10之间
if num >= 0 and num <= 10:
    print ('hello')

# 输出结果: hello
 
num = 10
# 判断值是否在小于0或大于10
if num < 0 or num > 10:
    print ('hello')
else:
    print ('undefine')

# 输出结果: undefine
 
num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):    
    print ('hello')
else:
    print ('undefine')
    
# 输出结果: undefine

num = 99
if (num  == 100): 
    print ("变量 num 的值为100") 
elif(num != 100):
    print ("变量 num 的值为", num) 
else:
    print("Opps")

# 输出:变量 num 的值为 99

Conditional can let the computer make their own choice, Python's if ... elif ... else very flexible.

Matching condition is determined from the top down, the corresponding block statement is executed when the condition is satisfied, and the subsequent elif else are no longer performed.

Guess you like

Origin www.cnblogs.com/hcf-fcl/p/11199554.html