Python - drawing a flower with turtle by recursive function











up vote
0
down vote

favorite












I am trying to draw a turtle-graphics flower with num petals. However, when I run my code I only get one single petal printed out. The error I get is under the def flower(num, i = 1) part of the code, but I am not sure how I can fix it.



import time
from turtle import *

pensize(2)
pencolor("orange")
bgcolor("green")
fillcolor("blue")
hideturtle()

def halfPetal():
forward(50)
left(30)
forward(75)
left(30)
forward(50)
left(120)

def petal():
for i in range(2):
halfPetal()

def flower(num, i=1):
if i==1:
begin_fill()
for i in range(num):
petal()
left(360/petal())
end_fill()

flower(12)
time.sleep(10)









share|improve this question
























  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. This generally includes the full error message.
    – Prune
    Nov 7 at 18:56










  • @Prune im pretty sure there is no error ... it just does not draw the image OP expcted... he did not do a good job of describing his expected output
    – Joran Beasley
    Nov 7 at 19:35






  • 1




    @JoranBeasley: the posted code faults with the error I cited. Also, note that OP says "The error I get ..."
    – Prune
    Nov 7 at 20:09















up vote
0
down vote

favorite












I am trying to draw a turtle-graphics flower with num petals. However, when I run my code I only get one single petal printed out. The error I get is under the def flower(num, i = 1) part of the code, but I am not sure how I can fix it.



import time
from turtle import *

pensize(2)
pencolor("orange")
bgcolor("green")
fillcolor("blue")
hideturtle()

def halfPetal():
forward(50)
left(30)
forward(75)
left(30)
forward(50)
left(120)

def petal():
for i in range(2):
halfPetal()

def flower(num, i=1):
if i==1:
begin_fill()
for i in range(num):
petal()
left(360/petal())
end_fill()

flower(12)
time.sleep(10)









share|improve this question
























  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. This generally includes the full error message.
    – Prune
    Nov 7 at 18:56










  • @Prune im pretty sure there is no error ... it just does not draw the image OP expcted... he did not do a good job of describing his expected output
    – Joran Beasley
    Nov 7 at 19:35






  • 1




    @JoranBeasley: the posted code faults with the error I cited. Also, note that OP says "The error I get ..."
    – Prune
    Nov 7 at 20:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to draw a turtle-graphics flower with num petals. However, when I run my code I only get one single petal printed out. The error I get is under the def flower(num, i = 1) part of the code, but I am not sure how I can fix it.



import time
from turtle import *

pensize(2)
pencolor("orange")
bgcolor("green")
fillcolor("blue")
hideturtle()

def halfPetal():
forward(50)
left(30)
forward(75)
left(30)
forward(50)
left(120)

def petal():
for i in range(2):
halfPetal()

def flower(num, i=1):
if i==1:
begin_fill()
for i in range(num):
petal()
left(360/petal())
end_fill()

flower(12)
time.sleep(10)









share|improve this question















I am trying to draw a turtle-graphics flower with num petals. However, when I run my code I only get one single petal printed out. The error I get is under the def flower(num, i = 1) part of the code, but I am not sure how I can fix it.



import time
from turtle import *

pensize(2)
pencolor("orange")
bgcolor("green")
fillcolor("blue")
hideturtle()

def halfPetal():
forward(50)
left(30)
forward(75)
left(30)
forward(50)
left(120)

def petal():
for i in range(2):
halfPetal()

def flower(num, i=1):
if i==1:
begin_fill()
for i in range(num):
petal()
left(360/petal())
end_fill()

flower(12)
time.sleep(10)






python turtle-graphics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 18:58









Prune

41.1k133454




41.1k133454










asked Nov 7 at 18:47









stabekk

31




31












  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. This generally includes the full error message.
    – Prune
    Nov 7 at 18:56










  • @Prune im pretty sure there is no error ... it just does not draw the image OP expcted... he did not do a good job of describing his expected output
    – Joran Beasley
    Nov 7 at 19:35






  • 1




    @JoranBeasley: the posted code faults with the error I cited. Also, note that OP says "The error I get ..."
    – Prune
    Nov 7 at 20:09


















  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. This generally includes the full error message.
    – Prune
    Nov 7 at 18:56










  • @Prune im pretty sure there is no error ... it just does not draw the image OP expcted... he did not do a good job of describing his expected output
    – Joran Beasley
    Nov 7 at 19:35






  • 1




    @JoranBeasley: the posted code faults with the error I cited. Also, note that OP says "The error I get ..."
    – Prune
    Nov 7 at 20:09
















Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. This generally includes the full error message.
– Prune
Nov 7 at 18:56




Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. This generally includes the full error message.
– Prune
Nov 7 at 18:56












@Prune im pretty sure there is no error ... it just does not draw the image OP expcted... he did not do a good job of describing his expected output
– Joran Beasley
Nov 7 at 19:35




@Prune im pretty sure there is no error ... it just does not draw the image OP expcted... he did not do a good job of describing his expected output
– Joran Beasley
Nov 7 at 19:35




1




1




@JoranBeasley: the posted code faults with the error I cited. Also, note that OP says "The error I get ..."
– Prune
Nov 7 at 20:09




@JoranBeasley: the posted code faults with the error I cited. Also, note that OP says "The error I get ..."
– Prune
Nov 7 at 20:09












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










At least one problem is at the line:



        left(360/petal())


petal does not return a value, so you're trying to divide by None. There is no such operation in Python, so you get a fatal error. Instead, I think you need to divide by the quantity of petals you're going to draw:



        left(360.0 / num)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195868%2fpython-drawing-a-flower-with-turtle-by-recursive-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    At least one problem is at the line:



            left(360/petal())


    petal does not return a value, so you're trying to divide by None. There is no such operation in Python, so you get a fatal error. Instead, I think you need to divide by the quantity of petals you're going to draw:



            left(360.0 / num)





    share|improve this answer

























      up vote
      2
      down vote



      accepted










      At least one problem is at the line:



              left(360/petal())


      petal does not return a value, so you're trying to divide by None. There is no such operation in Python, so you get a fatal error. Instead, I think you need to divide by the quantity of petals you're going to draw:



              left(360.0 / num)





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        At least one problem is at the line:



                left(360/petal())


        petal does not return a value, so you're trying to divide by None. There is no such operation in Python, so you get a fatal error. Instead, I think you need to divide by the quantity of petals you're going to draw:



                left(360.0 / num)





        share|improve this answer












        At least one problem is at the line:



                left(360/petal())


        petal does not return a value, so you're trying to divide by None. There is no such operation in Python, so you get a fatal error. Instead, I think you need to divide by the quantity of petals you're going to draw:



                left(360.0 / num)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 18:55









        Prune

        41.1k133454




        41.1k133454






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195868%2fpython-drawing-a-flower-with-turtle-by-recursive-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings