12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

48 Chapter 5. Conditionals and recursion1. Write a function named is_triangle that takes three integers as arguments, and that printseither “Yes” or “No,” depending on whether you can or cannot form a triangle from stickswiththe given lengths.2. Write a function that prompts the user to input three stick lengths, converts them to integers,and usesis_triangletocheck whether stickswiththegiven lengths can form atriangle.The following exercises useTurtleWorld from Chapter 4:Exercise 5.3 Read the following function and see if you can figure out what it does. Then run it(seetheexamples inChapter 4).def draw(t, length, n):if n == 0:returnangle = 50fd(t, length*n)lt(t, angle)draw(t, length, n-1)rt(t, 2*angle)draw(t, length, n-1)lt(t, angle)bk(t, length*n)Exercise 5.4 The Koch curve is afractal that looks something likethis:To draw aKoch curve withlength x, allyou have todois1. Draw aKoch curve withlength x/3.2. Turn left 60degrees.3. Draw aKoch curve withlength x/3.4. Turn right 120 degrees.5. Draw aKoch curve withlength x/3.6. Turn left 60degrees.7. Draw aKoch curve withlength x/3.The only exception isifxislessthan 3. Inthat case, you can justdraw astraight linewithlength x.1. Write a function called koch that takes a turtle and a length as parameters, and that uses theturtletodraw aKoch curve withthe given length.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!