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

5-The cycle of basic programming in Python

編輯:Python

PythonBasic programming cycle

  • while循環
  • for循環
  • 循環打斷
  • 循環嵌套
  • Pass語句
  • 小節練習
    • 3daffodils
    • 猜數字

概念:Do something repetitively.
使用場景:Want to reserver multiple times to perform certain operations;Want to iterate over a collection etc.
實現:while和forCycle in two ways.

while循環

語法1

while 條件:
The execution condition when the condition is met


語法2

while 條件:
The execution condition when the condition is met
else:
條件不滿足時,執行的代碼


例子

num=0
while num<10:
num+=1
print(num)
else:
print(num)# num一定是10
print("循環已結束")

for循環

語法1

for x in xxx:
循環語句
# 通常xxx是一個集合,xEach element in the collection will be taken out and assigned tox
# 在循環體中可以直接使用x的值

Take a traversal example

pets=["小紅","小藍","小黑","小白"]
for name in pets:
print(name)

結果為:

語法2

for x in xxx:
循環語句
else:
條件不滿足執行的語句
# 通常xxx是一個集合,xEach element in the collection will be taken out and assigned tox
# 在循環體中可以直接使用x的值

注意:Only loops that are not interrupted will jumpelse,即沒有使用break(下面將會講)

例1:反轉字符串

notice='PythonBasic programming cycle'
# 拆字
result = ''
for n in notice:
result=n+result
print(result)

輸出:The basics of cycle programmingnohtyP

例2:打印1-100之間的偶數

for num in range(2,101,2):
print(num)

range參數:左閉右開,First, last, last is the variance

循環打斷

break

interrupt this cycle,跳出整個循環

for i in range(1,11):
if i==5:
break
print(i)

只打印1-5

continue

結束本次循環,繼續執行下次循環

for i in range(1,11):
if i==5:
continue
print(i)

just don't print5

例:加法器(1-100間)

Make a simple adder,The user enters two numerical values,輸出對應的和,按q退出;If the user does not exit the program, it will continue to be used after the output is completed;If the data entered by the intermediate user is incorrect, an error prompt will be given and start from the beginning.

while True:
num1 = eval(input("請輸入第一個數值:"))
num2 = eval(input("請輸入第二個數值:"))
if num1>100 or num2>100:
print("你輸入的數據有問題,請重新輸入")
continue
result = num1+num2
print("The result of your calculation is :",result)
isQ = input("Do you want to quit(q:退出,其它:不退出,繼續)")
if isQ=='q':
break

輸出:

循環嵌套

Loop nested conditions

# 取1-100Divisible within3的數
for num in range(1,101):
if num%3==0:
print(num)

循環嵌套循環

外層循環執行一次,The inner loop executes all

for i in range(1,5):
for j in range(1,3):
print(j)

例子:九九乘法表

for num in range(1,10):
for n in range(1,num+1):
print("%d*%d=%d"%(n,num,n*num),end="\t")
print()

輸出:

Pass語句

概念:pass語句是空語句,Do nothing is generally used as a placeholder statement
作用:保持程序結構的完整性

age = 18
if age>18:
pass
else:
pass

I don't know the internal content yet, but I can write the structure firstpass驗證是否正確

小節練習

3daffodils

Requires verification of three-digit values,Just need to verify whether it is a three-digit value
百位的3次方+十位的3次方+個位的3次方=數值本身,如153=13+53+3**3
注:Ignore the case where the input is not a number for now

# 1.用戶輸入數值
num = eval(input("Please enter a three-digit number:"))
# 2.數據有效性驗證
if not(100<=num<=999):
print("你輸入的數據無效")
exit()
# 3.Determine if it is a daffodil
baiwei = num//100
shiwei = num%100//10
gewei = num%10
result = (baiwei**3 + shiwei**3 + gewei**3 == num)
# 4.輸出結果
if result:
print("%d,是水仙花數"%num)
else:
print("%d,不是水仙花數"%num)

猜數字

Ask the program to give a number,然後讓用戶來猜,If they are equal then it is correct,不相等,It will give a hint whether it is big or small.

# 1.准備數據
num=550
count=0
# 2.數據處理
while True:
result = eval(input("請輸入值:"))
count+=1
if result == num:
print("恭喜你答對了,答案是%d,你總共猜了%d次"%(result,count))
break
if result>num:
print("Guess a bit too big")
else:
print("Guess a little too small")


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