"""蓝色天空网 mbsky.com — 自编基础程序、数列与精确输出。"""
from pathlib import Path
import sys,io,contextlib,keyword
sys.path.insert(0,str(Path(__file__).resolve().parent))
from mbsky_pychecks import Checks
def fibonacci_below(limit):
a,b=0,1;result=[]
while b<limit:
result.append(b)
a,b=b,a+b
return result
def fibonacci_count(count):
a,b=0,1;result=[]
for _ in range(count):
result.append(b)
a,b=b,a+b
return result
def main():
c=Checks();output=io.StringIO()
with contextlib.redirect_stdout(output):print("蓝色天空网", "mbsky.com")
c.eq("brand and default separator",output.getvalue(),"蓝色天空网 mbsky.com\n")
c.eq("256 square",256*256,65536);c.eq("addition",3+2,5)
output=io.StringIO()
with contextlib.redirect_stdout(output):
i=256*256
print("数值:",i)
c.eq("print variable value",output.getvalue(),"数值: 65536\n")
topics=["HTML","CSS","JavaScript"]
for i,expected in enumerate(["HTML","CSS","JavaScript"]):c.eq("list item "+str(i),topics[i],expected)
output=io.StringIO()
with contextlib.redirect_stdout(output):
for number in range(5):print(number)
c.eq("five lines range0..4",output.getvalue(),"0\n1\n2\n3\n4\n")
for x,expected in [(6,"lower-or-equal"),(10,"lower-or-equal"),(11,"higher")]:
if x>10:result="higher"
else:result="lower-or-equal"
c.eq("threshold "+str(x),result,expected)
c.eq("Fibonacci below10",fibonacci_below(10),[1,1,2,3,5,8])
c.eq("Fibonacci below1 empty",fibonacci_below(1),[])
c.eq("Fibonacci below2 excludes2",fibonacci_below(2),[1,1])
c.eq("Fibonacci below0 empty",fibonacci_below(0),[])
c.eq("Fibonacci ten terms",fibonacci_count(10),[1,1,2,3,5,8,13,21,34,55])
c.eq("Fibonacci zero count",fibonacci_count(0),[])
c.eq("Fibonacci count not bound",fibonacci_count(10)!=fibonacci_below(10),True)
expected1000=[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987]
c.eq("Fibonacci below1000",fibonacci_below(1000),expected1000)
a,b=2,3;a,b=b,a+b;c.eq("parallel RHS sees old values",(a,b),(3,5))
a,b=2,3;a=b;b=a+b;c.eq("sequential assignment differs",(a,b),(3,6))
trace=[]
def mark(label,value):trace.append(label);return value
a,b=mark("left",3),mark("right",5)
c.eq("RHS left to right",trace,["left","right"])
c.eq("RHS values after evaluation",(a,b),(3,5))
a=0
def partial_assignment():
nonlocal a
a,empty[0]=1,2
empty=[]
c.raises("target assignment may fail later",IndexError,partial_assignment)
c.eq("earlier assignment not rolled back",a,1)
output=io.StringIO()
with contextlib.redirect_stdout(output):
a,b=0,1
while b<1000:
print(b,end=",");a,b=b,a+b
comma="1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,"
c.eq("actual comma no newline",output.getvalue(),comma)
c.eq("last comma retained",output.getvalue().endswith(","),True)
c.eq("no final newline",output.getvalue().endswith("\n"),False)
output=io.StringIO()
with contextlib.redirect_stdout(output):print(1,2,sep="|",end="!")
c.eq("separator and end independent",output.getvalue(),"1|2!")
c.eq("end not reserved keyword",keyword.iskeyword("end"),False)
c.eq("optional join alternative",",".join(map(str,fibonacci_below(10))),"1,1,2,3,5,8")
c.finish("python3-step1",fibonacci_limit1000=expected1000,exact_comma_output=comma,source_code_never_executed=True)
if __name__=="__main__":main()
本例需在 本地 Python 环境 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地 Python 环境
入口文件:projects/python-lab/python3-step1.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
python3-step1.py,或下载右侧课程工程包。 - 确认本地已准备 本地 Python 环境。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202611a463.zip
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202612a292.zip