Note in the table below,A special form of assignment operation
#if-elseCan be simplified with conditional expressions
print("恭喜中獎" if number==987456 else "未中獎")
range(n,m)函數,產生一個[n,m)的整數序列,包含n但不包含m;
range(stop): 從0到(stop-1)的數
range(start,stop,step):從start到(stop-1),步長為step
for i in "hello" :
print(i) #Output each letter
也是循環,Traversing objects are strings
while循環,In the statement block, add a statement that changes the loop variable,例如:i++
print():Output newlines directly(There are no parameters in parentheses)
for i in range(1, 6):
for j in range(1, i+1):
print("*", end="")
print() #Print out a right triangle
for i in range(1,6):
for j in range(1,7-i):
print("*",end="")
print() #輸出倒直角三角形
for i in range(1,6):
#倒直角三角形
for i in range(1,6):
for j in range(1,6-i):
print("&",end="")
print()
#1,3,5,7的三角形
for k in range(1,i*2):
print("*",end="")
#換行
print() #輸出等腰三角形
#判斷閏年平年
year=eval(input("Please output a four-digit year"))
if(year%==0 and year%100!=0) or (year%400==0):
print(year,"是閏年")
else:
print(year,"不是閏年")
#模擬10086查詢功能
#(1)初始化變量
answer="y"
#(2)Loop plus conditional judgment
while answer=="y":
print("--------Welcome to the query function---------")
print("1,查詢當前余額")
print("2,Query the current remaining traffic")
print("3,Query the current remaining call duration")
print("0,退出系統")
choice=input("輸入執行操作")
if choice=="1":
print("當前余額:450")
elif choice=="2":
print("當前剩余流量:4G")
elif choice=="3":
print("Remaining call time:300min")
elif choice=="0":
print("程序退出,感謝使用")
break
else:
print("輸入有誤,重新輸入")
#(3)改變變量
answer=input("continue opcode?y/n")
print("程序退出,謝謝使用")
#輸出九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print(str(j)+"*"+str(i)+"="+ str(i*j),end="\t")
print()
#猜數游戲
import random
rand=random.randint(1,100)
count=1 #記錄猜的次數
while count<=10:
number=eval(input("輸入猜的數"))
if number==rand:
print("猜對了")
break
elif number>rand:
print("偏大")
else:
print("偏大")
count+=1
#判斷次數
if count <=3:
print("聰明",count,"次數")
elif count<=6:
print("還可以",count,次數)
else:
print("次數偏多",count,次數)
Python Development of guidelin
@解決 img_gray = cv2.cvtColor(im