程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python process control structure ---if else, while, for

編輯:Python

python Process control structure —if else 、while、for


# One 、 Choose the process ————if Select branch statement 
''' Single branch if Conditional expression : Comparison operator Logical operators / Conditional expression that matches Code instructions .... '''
score=60
if score <=60:
print(" Your score is :\n%d"%(score))
print('failed')
pass # Empty statement 
print(' End of statement ')
''' --------------------------------- Double branch if Conditional expression : Comparison operator Logical operators / Conditional expression that matches Code instructions .... else: Code instructions '''
score=60
if score>60:
print("successed")
pass
else:
print("failed")
pass
--------------------------
''' # Multiple branches #1. Each condition is mutually exclusive #2. There are at least 2 In this case #elif Be sure to write conditions and statements #else Optional , As far as possible with , Avoid omission '''
score=int(input(" Please enter your score :"))
if score<60:
print("failed")
pass
elif score<80:
print("ok")
pass
elif score<90:
print("good")
pass
else:
print("very good")
pass
print(" Make persistent efforts , Have a good holiday !")
# Guessing game 0- stone 、1- scissors 、2- cloth 
import random
# Computer people 
person=int(input(" Please punch :[0- stone 、1- scissors 、2- cloth ]"))
computer=random.randint(0,2)
if person ==0 and computer==1: # Multiple conditions 
print("win")
pass
elif person==1 and computer ==2:
print("win")
pass
elif person==2 and computer ==0:
print("win")
pass
elif person==computer:
print("no win no lost")
else:
print("lost")
#if else The nested use of 
xuefen=int(input(" Please enter your credits :"))
grade=int(input(" Please enter your score :"))
if xuefen>10:
if grade>80:
print(' Promotion ')
pass
else:
print(" Enough credits , The result is not up to standard ")
pass
else:
print(" Credit early warning ")
pass
# Two 、 Loop statement 
# Classification of cycles :while、for
#1)while Conditional expression :
# Code instructions 
''' *** Grammatical features *** 1. There is an initial value 2. Conditional expression 3. Variable 【 Circulating body count variable 】 Self increasing or self decreasing , Otherwise, it will cause a dead cycle # Conditions of use : The number of cycles is uncertain , Rely on loop conditions to end # Purpose : To make similar or identical code operations simpler , Make your code reusable # for '''
# Output 1-100 Data between 
index=1 # Define a variable , You have to assign 
while index<=100:
print(index)
index+=1
pass
# many times Guessing game 0- stone 、1- scissors 、2- cloth 
import random
# Computer people 
count=1
while count<=10:
person=int(input(" Please punch :[0- stone 、1- scissors 、2- cloth ]"))
computer=random.randint(0,2)
if person ==0 and computer==1: # Multiple conditions 
print("win")
pass
elif person==1 and computer ==2:
print("win")
pass
elif person==2 and computer ==0:
print("win")
pass
elif person==computer:
print("no win no lost")
else:
print("lost")
count+=1
pass
# Print the multiplication table 
row=1
while row<=9:
col=1
while col<=row:
print("%d*%d=%d"%(row,col,row*col),end=" ")
col+=1
pass
print()
row+=1
pass
row=9
while row>=1:
col=1
while col<=row:
print("%d*%d=%d"%(row,col,row*col),end=" ")
# end=' ' The function of is to make the output result do not wrap , Space off 
col+=1
pass
print() # Line break 
row-=1
pass
# Print right triangle 
row=7
while row>=1:
j=1
while j<=row:
print("*",end=' ')
j+=1
pass
print()
row-=1
pass
row=1
while row<=5:
j=1
while j<=5-row: # Control the number of spaces to be printed 
print(' ',end=' ')
j+=1
pass
k=1
while k<=2*row-1: # Control printing * Number 
print('*',end=' ')
k+=1
pass
print()
row+=1
pass
2)#for loop 
# Grammatical features : Traversal operation , Take each value in the collection container in turn 
#for Temporary variable in Containers :
# Execute code block 
tags=' I am a Chinese ' # The string type itself is a collection of character types 
for item in tags:
print(item)
pass
#range function : Generate data set list 
#range( Starting value , End value , step ) # The step size is not 0, The default is 1
sum=0
for data in range(1,101): # Left closed right away 
sum+=data # Sum up 
#print(data,end=' ')
pass
print("sum=%d"%sum)
print(-------------------------for Use -----------)
for data in range(50,201):
if data %2==0:
print('%d It's even '%data,end=' ')
pass
else:
print("%d Is odd "%data)
pass
pass
#3)break continue
#break Represents the end of the interrupt If the conditions are met, the cycle of this layer will be ended directly 
#continue: End this cycle , Continue with the next cycle 
# These two keywords can only be used in a loop 
#break Use 
sum=0
for item in range(1,51):
if sum>100:
break
pass
sum+=item
pass
print("sum=%d"%sum)
#continue Use 
sum=0
for item in range(1,101):
if item%2!=0: # Directly jump out of this layer if the conditions are met 
continue
print("continue None of the following will be executed ")
pass
print(item) # If you are not satisfied print
pass
for item in 'i love python':
if item=='e':
break
pass
print(item)
pass
for item in 'i love python':
if item=='e':
continue
pass
print(item)
index=1
while index <=100:
if index >20:
break
pass
print(index,end=' ')
index+=1
for i in range(1,10):
for j in range(1,10):
if i>=j:
print("%d*%d=%d"%(i,j,i*j),end=' ')
pass
if i<j:
break
pass
print() # Control line feed 
pass
for: else: #else It's also for Part of 
as long as for In the break,else They don't execute , Didn't show up else,else Will execute
account='wf'
pwd='123'
for i in range(1,3):
zh=input(" Please enter your account number :")
mm=input(" Please input a password :")
if account==zh and pwd==mm:
print("loading...")
break # Exit this layer loop , Below else It's also for Part of , therefore else And don't execute 
pass
pass
else:
print("error")
pass
#while else as long as while There is no break,else Will execute 
# As long as it appears break,else They don't execute 
index=1
while index<=10:
print(index)
if index==6:
break
pass
index+=1
pass
else:
print("else Carried out? ")
#day2 Exercises 
''' Guess age games , There are three requirements 1. Allow users to try up to 3 Time 2. Every attempt 3 Next time , If you haven't guessed right , Just ask the user if he still wants to play , If the answer is Y or y, Just keep it guessing 3 Time , In reciprocating , If the answer is N or n, Just quit the program 3. How to guess right , Just quit '''
import random
age=random.randint(0,3)
print(age)
j=3
for i in range(1,4):
answer=int(input(" Please enter your answer :"))
if age==answer:
print("bingo")
break
pass
j-=1
print(" You have %d The opportunity to "%j)
pass
else:
print(" You failed ")
times=0
count=0
while times<=3:
age=int(input(" Please enter your answer :"))
if age==25:
print("bingo")
break
pass
elif age>25:
print(" It's too big ")
pass
else:
print(" Is too small ")
pass
times+=1
if times==3:
choose=input(" You continue to guess Y/N?")
if choose=='Y' or choose=='y':
time=0 # Reset times For the initial value 
pass
if choose=='N' or choose=='n':
times==4
pass
else:
print(" Please enter the correct tag ")
pass
pass
pass
''' # Exercises 2 Xiao Wang's height 1.75, weight 80.5kg. Please according to BMI The formula ( Weight divided by the square of height ) Help Xiao Wang calculate his BMI Index , And according to BMI Index : lower than 18.5 Over light 18.5-25: normal 25-28: overweight 28-32: obesity higher than 32: Severe obesity use if-elif Judge and print results '''
height=1.75
weight=80.5
BMI=weight/(height**2)
if BMI<=18.5:
print("BMI:%d, Over light "%BMI)
pass
elif BMI<=25:
print("BMI:%d, normal " %BMI)
pass
elif BMI<=32:
print("BMI:%d, overweight " %BMI)
pass
else:
print("BMI:%d, Severe obesity " %BMI)
pass

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved