Branch statement if
The best show if the branch is a small program that will be converted to level the score
because it is a relatively simple small program, so we are given directly Code:
score=int(input("请输入成绩: "))
if (90<=score<=100) :
print('A')
# 100>=score>=90 也可以
if (80<=score<90) :
print('B')
if (60<=score<80) :
print('C')
if (0<=score<60) :
print('D')
if (score<0 or score>100) :
print("输入错误!")
note:
- Py may be present in the continuous inequality: 100> = score> = 90
- Any data obtained from the keyboard, attention should specifically converted to the corresponding type
- C++:
||
<==> Python:or
But anyone with a programming foundation of the students are not willing for this
such an approach is not enough ta beautiful: The five if statement is completely parallel, respectively, will be in the implementation of the program of inequality judgment
even score satisfies one of the conditions, still if it takes time to be determined later, in other words is a constant greater
In other languages, we used if...else
to solve this problem
the same, Py is also a if...else
statement:
score=int(input("请输入成绩: "))
if (90<=score<=100) :
print('A')
else :
if (80<=score<90) :
print('B')
else :
if (60<=score<80) :
print('C')
else :
if (0<=score<60) :
print('D')
else :
print("输入错误!")
note:
- To indent a beautiful and neat (which coding style for treacherous and pressure lines like me too unfriendly to the TwT)
There is one that one, this indent true horror
predecessors estimated development Py also discovered the terrible truth, so invented a language different from other branches of the syntax :
actually else + if, also very simple to realize functions:
score=int(input("请输入成绩: "))
if (90<=score<=100) :
print('A')
elif (80<=score<90) :
print('B')
elif(60<=score<80) :
print('C')
elif (0<=score<60) :
print('D')
else :
print("输入错误!")
~ ~ ~ ~ Ah, so many beautiful Well
this syntax fully embodies the characteristics of the two languages Py: high readability concise &&
Ternary operator
Remember cinnabar mole in C ++ - Three pre-project operator - Well?
int x=4,y=5;
int mx=(x>y) ? x : y;
In fact Py has a similar syntax, I first saw the prototype of ta can not help wondering: Is not this reduces the readability of the Well / laugh cry
but is said to have greatly Py forum expressed a strong appeal, Therefore, version 2.5, developers joined ternary operator:
Syntax: x if (condition) else y
To meet the conditions to take x, y or take
x,y=4,5
if (x > y)
mx = x
else
mx = y
############
x,y=4,5
small = x if (x>y) else y
Assert assert
Finally, the last, we introduce the relatives of an if statement: assert assert
when the keyword behind the condition is false, the program automatically collapse and throw exceptions AssertionError
In general, we can use ta checkpoint is inserted in the program, the program in a certain condition is true the program to work properly when you need to make sure, assert keyword is very useful
while loop
while 条件:
循环体
for loop
Py for loop is more flexible and powerful than C ++
for 目标 in 表达式:
循环体
# 表达式一般都是一个列表,列表的定义在下面会有展示
Or actual operation:
Now the question coming:
if we are to achieve the C ++ language for (int i=1;i<=n;i++)
features, you do not need to open a list of length n, right?
Of course not friends, here we introduce a small partner for loop:
Prototype:
then [] may be omitted contents inside
this function include complete form three parameters: the starting point, the focus, the stride
general,the stride is the default step 1, the starting point is the default start 0
this effect is to generate a function from start to stop, tolerance step list of numbers
Usage is very simple:
The last offer chowder demo:
bingo='周杰伦'
print("Coco_T_最喜欢的歌星是谁呀~")
while 1 :
ans=input()
# str(input()) input默认返回的是str类型,所以不需要类型转换
if (ans == bingo) :
break
else :
print('在猜猜啦')
print('哎呦~不错哦')
(Was terrible, Py thinking and C ++ is not the same ah, look at this program, want to be able to get it)