python study notes 05- conditional branch and loop 1

Thinking: 100-point, 90 points or more A, 80-90 of B, 60-80 for the C, 60 hereinafter is D, when the user enters grades, corresponding to print letters

TEMP = INPUT ( ' Please enter your score: ' ) 
Score = int (TEMP)
 IF 100> = Score> = 90 :
     Print ( ' A ' )
 elif 90> Score> = 80 :
     Print ( ' B ' )
 elif 80 > Score> = 60 :
     Print ( ' C ' )
 elif 60> Score> = 0:
     Print ( ' D ' )
 the else :
     Print ( 'Make a mistake! ' )

Very simple small cycles, but this indentation errors made me a headache ah ...

Episode - General indentation errors microscopic naked eye, you can be set up to look at it on a notepad nice to see, especially the mix of spaces to indent the case

            You can also use Pycharm to run, you can know which lines are being given.

Return on title, elif = else if

You can also use a more intuitive way to write, for example:

temp=input('请输入你的成绩:')
score=int(temp)
if 100 >= score >= 90:
    print('A')
if 90 > score >= 80:
    print('B')
if 80 > score >= 60:
    print('C')
if 60 >=score >= 0:
    print('D')
if score > 100 or score < 0:
    Print ( ' input error! ' )

The difference is the use of esif saves CPU time, the number of the latter method even if the first input can be judged, it will be performed again later if all the results will be printed

After the esif judged result is determined not to perform subsequent

python can effectively avoid the suspension esle

E.g:

if(h1>2):
  if(h2>7):
     print()
esle:
  print()

if c language - esle there is the principle of proximity. The final esle would follow if the second will be evaluated

The python indentation strict specifications determined by the code indentation which if else to follow to execute the program, thereby avoiding some of the root causes of the problem easily overlooked

The ternary operator pytho

Syntax: the X-   IF condition the else the y-

When the output condition x is True, False condition is output when the value y

for example:

x,y=4,5

if x<y:

  small=x

else:

  small=y

### small= x if x<y else y

Assertion (assert)

When the latter assert keyword condition is false when the program will automatically collapse, and throw exceptions AssertionError

This will be required codes blew it under what circumstances? ?

In general we can use it in the inspection point in the program, you need to ensure that the procedures in certain conditions must be true in order for the program to work, then, assert keyword is very useful

 

Membership operator

Python has a membership operator: in, to check whether a value is in the sequence, if the sequence returns True, otherwise False.

 

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/fengchuiyangliu/p/11299141.html