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>
Call the functions? (after defining them) Code (Text): initialA(0) initialB(0) Maybe 0 isn't appropriate for size, you know better than I.
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?
@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.
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.
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?
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): g = input("Input the day of the month your birthday is on: ") day = int(g) if (day >= 1 and day <= 9): #We just need to do the multiply by 3 and print it luckyDay = day * 3 print luckyDay elif (day >= 10 and day <= 31): digitTwo = day % 10 #Pull the last digit off the number, set it to digit two using modulus digitOne = day / 10 #Set the other digit to digit one by dividing by ten, python discards the decimal value as this is an integer if (digitOne == digitTwo): #They're equal in value, do a multiplication luckyDay = digitOne * digitTwo else: #They're different in value, do an addition luckyDay = digitOne + digitTwo print luckyDay #Print the result else: print 'Not a valid entry!' #The user is trying to be a smart-arse and put something invalid in
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)
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
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..
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): #Letter A pendown() forward(100) right(90) forward(50) right(90) forward(50) right(180) forward(50) right(90) forward(50) penup() #Travel to next start point left(90) forward(50) left(90) #Letter B pendown() right(45) forward(50) left(90) forward(50) right(90) forward(50) left(90) forward(50) left(135) forward(100) #This might need adjusting to reach the bottom properly in terms of length penup() Which should draw something like: Code (Text): - |-| |> | | |>