Home > other >  High school students zero based learning python (2)
High school students zero based learning python (2)

Time:10-22

learning python
Everybody is good, we then left yesterday question now

 a=input (" weight ") 
B=input (" height ")
Weight=float (a)
Height=float (b)
Bmi=weight/height * * 2
If bmi<18.5:
Print (" low ")
Elif 18.5 & lt;=bmi>=25:
Print (" normal ")
Elif 25 & lt; Bmi> 28:
Print (" overweight ")
Elif 28 & lt;=bmi>=32:
Print (" obesity ")
The else:
Print (" severe obesity ")


Problem is in the process of executing code, no matter how the input values, the results have been and normal two kinds of results,

In the BBS, after Posting a warm-hearted friends code replied to me, the reason is,,,,,,,,


I write the wrong comparison of symbols, unexpectedly is on both sides, or estimate I had first seen the elementary school mathematics teacher beat me to death,

new knowledge
Next, start to learn loop statement
python there are two kinds of the cycle of for... in... and while

Where is the difference between the two?

Let's look at the for... in...
Reference
, for example, I want to put my name of the class, in turn, print it out, but dozens of print () will have to be written to the end of time, I'm afraid this time for... in... will come in handy!


Here to my closest three classmates, for example,

 names=[" little red ", "Ming", "li hua"] 
For the name names in:
Print (name)


Execute this code, will print the names of each element in turn:

Little red
Xiao Ming
Li hua

So the for x in... cycle is to make each element substitution variable x , then execute the indentation block statements,

Next is the while
cycleReference
, for example, I want to calculate the sum of the even within 10, can do it with a sum variable accumulative

 sum=0 
N=10
While n> 0:
Sum=sum + n
N=n - 2
Print (sum)


Results the correct

30
 

break
If we want to end the program in advance, such as sum & gt; End when I was 10, break statement can do it

 sum=0 
N=10
While n> 0:
Sum=sum + n
If sum> 10:
Break
N=n - 2
Print (sum)


OK, run results right

18
 

break is used in circulation,

continue
reference
if we want to print Numbers from 1 to 10, but only print odd, how to do? Let us use the continue try


 n=0 
While n & lt; 10:
N=n + 1
If n % 2==0: # if n is even, a continue statement
The continue # the continue statement will continue to the next round of cycle, directly behind the print () statement will not perform
Print (n)


Results there is no problem

 1 
3
5
7
9


Visible continue is used in this round of circulation, and directly to the next round,

The
reference
the distinction between attention and break!


Today's learning here! We all go to bed early!

CodePudding user response:

Suggest you can write CSDN blog, come on
  • Related