- Python極客項目編程(第2版)
- (美)馬赫什·文基塔查拉姆
- 358字
- 2024-07-24 11:09:21
1.3.4 編寫函數main()
函數main()創建并設置一個turtle對象,再調用drawKochSF()。
def main(): print('Drawing the Koch Snowflake...') t = turtle.Turtle() t.hideturtle() # 繪制科赫雪花 try: ? drawKochSF(-100, 0, 100, 0, t) ? drawKochSF(0, -173.2, -100, 0, t) ? drawKochSF(100, 0, 0, -173.2, t) ? except: print("Exception, exiting.") exit(0) # 等用戶在屏幕上單擊后退出 ? turtle.Screen().exitonclick()
從圖 1.4 可知,要繪制3個片段,確保最終輸出為六角對稱的雪花圖形。為此,調用了drawKochSF() 3次:對于第1個片段,點A和點B的坐標分別為(?100, 0)和(100, 0)?;對于第2個片段,坐標為(0, ?173.2)和(?100, 0)?;對于第3個片段,坐標為(100, 0)和(0, ?173.2)?。請注意,這些坐標與前面在程序test_turtle.py中繪制三角形時使用的坐標相同。請嘗試確定這些坐標是如何計算出來的。(提示:)
為捕獲繪圖期間可能發生的異常,將對函數drawKochSF()的調用放在一個Python try塊中。例如,用戶在繪圖期間關閉了窗口,將引發異常,可在except塊中捕獲此異常?,然后輸出一條消息并退出程序。如果用戶沒有終止繪圖過程,將執行代碼turtle.Screen().exitonclick()?,等待用戶單擊將窗口關閉。
推薦閱讀
- PostgreSQL for Data Architects
- Flink SQL與DataStream入門、進階與實戰
- Learning RabbitMQ
- Scala Design Patterns
- Getting Started with Python Data Analysis
- PhoneGap:Beginner's Guide(Third Edition)
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Multithreading in C# 5.0 Cookbook
- NetBeans IDE 8 Cookbook
- Getting Started with Eclipse Juno
- OpenGL Data Visualization Cookbook
- Qlik Sense? Cookbook
- Lift Application Development Cookbook
- Moodle 3 Administration(Third Edition)
- Android系統下Java編程詳解