User interaction conditional -if loop -while

 

1. The user interaction

  input (prompt) will return to what you type

  input string is accepted content

  name = input ( "Please enter your name:") # the program will stop here, waiting for user input

  s = "My name is" + name + ", this year 18-year-old"      string     #name string

  print(s)

  print (1+ "ha ha") does not want to add the correct      print ( "1" + "ha ha")

 a = input ( "Enter a:")

  b = input ( "Enter b:")

  print(a+b)→1020(a=10,b=20)

  # String into int

  #int(str)

  c=int(a)

  d = int (b) # non-numeric type can not appear

  print(a+b)→30

  

  # Constants: Variables can not change, there is no absolute constants in python, everyone convention, all the variables in capital letters is constant

  PI=3.1415926

  SYLAR_BIRTHDAY=1991

  print(PI)

  # Print statement

  print ( "12", "13") "," output will be a space

  a=10

  b=20

  print("a+b=",a+b)→ a+b=  30

2. conditional -if

  Create a new folder if.py

  money=300

 # If: if

 # Money> 500 conditions

 #: Start block

   # A Blank: tab (indentation) - block (slave and if)

   #If stuff out

   if money>500

     print ( "taxi home")

     print ( "home")  

     Home if money = 800 Ze taxi home home

grammar

    if conditions

     Block

    Description: When the (true) condition is established, the code block is executed

  money=800

  if   money>500:

    print ( "drink a little wine")

  else: # content table Otherwise, when the condition is not satisfied

    print ( "water")

grammar

   if conditions:

     Block

   else:

     Block

   money=8000

   if   money>3000:

     print ( "big sword")

   elif   money>2000:

     print ( "feet City")

   elif  money>1000:

     print ( "drink a little wine")

   else: # will not go

     print ( "home water")

grammar

   if Condition 1:

     Block

   elif Condition 2:

     Block

   Elif ...

   else:

  When the condition 1 is satisfied, the execution code 1, the condition 1 is not satisfied, it is determined again 2, and so on (which will execute a block of code)

  if  sex=="女":

    if  int(age)<20:

      print ( "open the door, how?")

    else:

      print ( "not open")

    print ( "Oh OMG")

  else:

    print ( "Goodbye")

3. loop --while

   New while.py

  flag=True

  count=1

  while   flag:

     print ( "1 glare")

     print ( "what you see")

     print ( "see you gnaw")

     print("上")

     count = count + 1 #count back increment

     if  count==6:

       flag=False

         print ( "eat")

If there is no circulation would have been if, Ctrl + c to stop

 grammar

  while conditions:

    Block

  Description: determining whether the condition is true, if true, the code block is executed (loop) continue executing the determination condition is true, if true, execution continues until the condition is false, the cycle is stopped

The number of problem number #

  index=1

  while  index<101:

    print(index)

    index=index+1

 

  1+2+3+4+......+100=?

  index=1

  sum=0

  while   index<101:

    sum=sum+index

    index=index+1

  print(sum)

4.break

  #break: interrupt cycle, a cycle completely stopped (turning off the current cycle of this layer)

  #coutinue: stopped this cycle, the next cycle continues

index=1

while   index<101:

  print(index)

  index=index+1

  if   index==88

    stopped circulating break #

 

 

 

 

index=1

while   index<101:

  if   index==88:                                                                                  if   index!=88:                                           

    (Index = index + 1) only 88

    continue

  print(index)

  index=index+1

   Without brackets in the content, only to 87

      

 

 

Guess you like

Origin www.cnblogs.com/dlx-code/p/10951903.html