don't click here

Help with some Python coding

Discussion in 'Technical Discussion' started by Peruant, Sep 11, 2010.

  1. Peruant

    Peruant

    Just dropping in through gaps Member
    Well see today's HW was to draw our initials using turtle graphics. Of course I typed in each direction line by line and it comes out. The code is like so

    <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>from turtle import*
    pendown()
    right(90)
    forward(100)
    backward(50)
    left(90)
    right(90)
    forward(100)
    backward(150)
    right(90)
    forward(40)
    forward(10)
    right(90)
    penup()

    left(360)
    pendown()
    forward(100)
    right(90)
    forward(100)
    backward(100)
    right(90)
    forward(30)
    right(90)
    left(180)
    forward(100)
    backward(100)
    pendown()
    forward(60)
    forward(50)
    right(90)
    left(180)
    forward(80)
    right(90)
    left(180)
    forward(120)
    right(270)
    forward(100)
    backward(20)
    right(270)
    forward(70)
    right(270)
    forward(85)</div>But the problem is that I can't get them to draw the letters correctly after defining them in the program. I know that the command to repeat the drawing , but the way I defined it is out of wack. Code's on the bottom). Anyone know a solution?

    <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>def initialA(size):
    pendown()
    right(90)
    forward(100)
    backward(50)
    left(90)
    right(90)
    forward(100)
    backward(150)
    right(90)
    forward(40)
    forward(10)
    right(90)
    penup()

    def initialB(size):
    left(360)
    pendown()
    forward(100)
    right(90)
    forward(100)
    backward(100)
    right(90)
    forward(30)
    right(90)
    left(180)
    forward(100)
    backward(100)
    pendown()
    forward(60)
    forward(50)
    right(90)
    left(180)
    forward(80)
    right(90)
    left(180)
    forward(120)
    right(270)
    forward(100)
    backward(20)
    right(270)
    forward(70)
    right(270)
    forward(85)
    </div>
     
  2. FraGag

    FraGag

    Tech Member
    Call the functions? (after defining them)
    Code (Text):
    1. initialA(0)
    2. initialB(0)
    Maybe 0 isn't appropriate for size, you know better than I.
     
  3. Elektro-Omega

    Elektro-Omega

    Mushroom Hill'in Member
    400
    2
    0
    UK
    -
    I studied Python not too long ago.

    I'm going to agree with FraGag, you probably need to call them.

    You have defined the function (told it what to do) but now you have to initialte the program to use the function.

    I'm not exactly sure how though, sorry.

    By the way, if you wanted to draw letters couldn't you have used the turtle.write function?

    Edit - I just noticed in your bottom code that you have pen down written but you don't have pen up written before your next pendown. Would the two pen down commands be causing a problem?
     
  4. Peruant

    Peruant

    Just dropping in through gaps Member
    @Electro - Probably the pen commands are causing it now that I think about it. I wasn't aware of the turtle.write function. Looks like the TA didn't go over that in class. Gonna try that out.

    @FraGag I assumed if I left it at 0 then it start somewhere. But still, can't get it to draw in different spots.

    Well edit later to see if it works. And I have another question but I'll post later.
     
  5. FraGag

    FraGag

    Tech Member
    You've defined a "size" argument but you're not using it at all, so whatever value you specify will be ignored. You'll have to edit your function to move the turtle somewhere or make it draw longer lines, or something like that.
     
  6. Peruant

    Peruant

    Just dropping in through gaps Member
    Alrighty got it working, it took me a while. Thanks for that one.

    Now then, my second question has to to do with if/else statements. I got the first part of it done but the next part is throwing me off..am I suppose to have two integers or one? Here's how I started it off:

    <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>g = input("Input first digit of your birthday: ")
    day = int(g)</div>

    If the birth day has one digit, the lucky day is that digit times 3.

    If the birth day has two digits and they are equal, the lucky day is the product of
    the two digit

    If the birth day has two digits and they are not equal, the lucky day is the sum of
    the two digit

    How do I code that and get it to display the result?
     
  7. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,233
    969
    93
    Long-term happiness
    Surely that should be "input the day of the month your birthday is on" if it's going to be just more than one digit?

    Anyway, for the sake of clarity and considering a birthday can only fall within a 31 day range, I hacked this together. It could probably be improved, but eh:

    Code (Text):
    1. g = input("Input the day of the month your birthday is on: ")
    2. day = int(g)
    3.  
    4. if (day >= 1 and day <= 9):&nbsp;&nbsp;&nbsp;&nbsp;#We just need to do the multiply by 3 and print it
    5. &nbsp;&nbsp;&nbsp;&nbsp;luckyDay = day * 3
    6. &nbsp;&nbsp;&nbsp;&nbsp;print luckyDay
    7. elif (day >= 10 and day <= 31):
    8. &nbsp;&nbsp;&nbsp;&nbsp;digitTwo = day % 10 #Pull the last digit off the number, set it to digit two using modulus
    9. &nbsp;&nbsp;&nbsp;&nbsp;digitOne = day / 10 #Set the other digit to digit one by dividing by ten, python discards the decimal value as this is an integer
    10. &nbsp;&nbsp;&nbsp;&nbsp;if (digitOne == digitTwo):&nbsp;&nbsp;&nbsp;&nbsp;#They're equal in value, do a multiplication
    11. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;luckyDay = digitOne * digitTwo
    12. &nbsp;&nbsp;&nbsp;&nbsp;else:&nbsp;&nbsp;&nbsp;&nbsp;#They're different in value, do an addition
    13. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;luckyDay = digitOne + digitTwo
    14. &nbsp;&nbsp;&nbsp;&nbsp;print luckyDay&nbsp;&nbsp;&nbsp;&nbsp;#Print the result
    15. else: print 'Not a valid entry!'&nbsp;&nbsp;&nbsp;&nbsp;#The user is trying to be a smart-arse and put something invalid in
     
  8. Peruant

    Peruant

    Just dropping in through gaps Member
    Wow, thanks a million Overlord. I knew those % signs would have to been used somehow. Let's see the elif statement I never seen in class but I'm going to read up on some more on the web because the book doesn't seem to have the information,at all. (I think)

    About that turtle.write function, had it working until my comp shut off suddenly. I just need to write the letters AB 3 times side by side. (Please excuse my asking since I still new to it and my teacher doesn't really explain the work)
     
  9. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,233
    969
    93
    Long-term happiness
    elif is just short for "else if", that's used if the first if statement doesn't match but you want another check before the final catch-all else. In this case it's essentially saying IF it meets the first condition do the first one, ELSE IF it meets the second condition it does the second, ELSE it does the last.

    I did have a look at your first function, but I didn't reply as I couldn't see how the hell those instructions could write the letters you said it did =P
     
  10. Peruant

    Peruant

    Just dropping in through gaps Member
    lol Well, those commands were like to move the turtle around but I guess it didn't work with the lines. Now I can get it working with a square easily since all it takes are rights and forwards, and then defining it. However the letters part is completely weird as you just read the code. Any simpler way to write the letters? You can ignore the code, and I can figure out what you did.

    And those comments in the lucky day code, it's a BIG help. But I wonder why there were no examples using the elif statement in the lecture..
     
  11. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,233
    969
    93
    Long-term happiness
    Well, if I was writing the LOGO-style code to do "AB", I'd just do it like, uh (code assumes you start facing upwards on the screen, add an initial right(whatever) in where needed):

    Code (Text):
    1. #Letter A
    2. pendown()
    3. forward(100)
    4. right(90)
    5. forward(50)
    6. right(90)
    7. forward(50)
    8. right(180)
    9. forward(50)
    10. right(90)
    11. forward(50)
    12. penup()
    13.  
    14. #Travel to next start point
    15. left(90)
    16. forward(50)
    17. left(90)
    18.  
    19. #Letter B
    20. pendown()
    21. right(45)
    22. forward(50)
    23. left(90)
    24. forward(50)
    25. right(90)
    26. forward(50)
    27. left(90)
    28. forward(50)
    29. left(135)
    30. forward(100) #This might need adjusting to reach the bottom properly in terms of length
    31. penup()
    Which should draw something like:

    Code (Text):
    1.  -
    2. |-| |>
    3. | | |>