import sys import turtle import ttk NORM_FONT = (\"Helvetica\", 10) def popupmsg
ID: 662699 • Letter: I
Question
import sys
import turtle
import ttk
NORM_FONT = ("Helvetica", 10)
def popupmsg(msg):
popup = ttk.Tk()
popup.wm_title("!")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
popup.mainloop()
def border(t, screen_x, screen_y):
# move the turtle to the center
turtle.penup()
turtle.home()
# Moving to lower left corner of the screen
turtle.forward(screen_x / 2)
turtle.right(90)
turtle.forward(screen_y / 2)
turtle.setheading(180)
# Drawing the border
turtle.pencolor('red')
turtle.pendown()
turtle.pensize(10)
for distance in (screen_x, screen_y, screen_x, screen_y):
turtle.forward(distance)
turtle.right(90)
turtle.penup()
turtle.home()
def square(t, size, color):
turtle.pencolor(color)
turtle.pendown()
for i in range(4):
turtle.forward()
turtle.right(90)
def main():
# Create screen and turtle.
screen = turtle.Screen()
screen_x, screen_y = screen.screensize()
t = turtle.Turtle()
# Drawing a border around the canvas
border(t, screen_x, screen_y)
x =int(input('input x coordinate'))
y =int(input('input y coordinate'))
if int(y)==y & int(x)==x:
# Draw a set of squares with varying color.
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
t.pensize(3)# size of the pen
for i, color in enumerate(colors):# for loop till all colors exhaust
square(t, (screen_y / 2) / 10 * (i+1), color) # determining dimensions of squares
print('Hit any key to exit')
dummy = input()
else:
popupmsg()
if __name__ == '__main__':
main()
Theres a problem with the else: line at the very bottom. It says syntax error and I have tried tons of different indentations and I have no idea what to do to fix it. Also my turtle.forward() used to be turtle.forward(size) but kept saying syntax error so I changed it and now it works for some reason. I am using python 3.4 and was just wondering if someone could help with my syntax error in the else line.
Explanation / Answer
The indentation is rectified at else.The problem is according to your code, 'if loop' ended before the 'for loop' present there. So, for loop is placed inside the 'if'.And 'for loop' above turtlr.forward(size) should be placed in the function 'square', you placed it outside of the function.So error occured. popmsg function requires an argument to be passed.Pass an argument.
Changes are presented in bold.
Program:
import sys
import turtle
import ttk
NORM_FONT = ("Helvetica", 10)
def popupmsg(msg):
popup = ttk.Tk()
popup.wm_title("!")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
popup.mainloop()
def border(t, screen_x, screen_y):
# move the turtle to the center
turtle.penup()
turtle.home()
# Moving to lower left corner of the screen
turtle.forward(screen_x / 2)
turtle.right(90)
turtle.forward(screen_y / 2)
turtle.setheading(180)
# Drawing the border
turtle.pencolor('red')
turtle.pendown()
turtle.pensize(10)
for distance in (screen_x, screen_y, screen_x, screen_y):
turtle.forward(distance)
turtle.right(90)
turtle.penup()
turtle.home()
def square(t, size, color):
turtle.pencolor(color)
turtle.pendown()
for i in range(4):
turtle.forward(size)
turtle.right(90)
def main():
# Create screen and turtle.
screen = turtle.Screen()
screen_x, screen_y = screen.screensize()
t = turtle.Turtle()
# Drawing a border around the canvas
border(t, screen_x, screen_y)
x =int(input('input x coordinate'))
y =int(input('input y coordinate'))
if int(y)==y & int(x)==x:
# Draw a set of squares with varying color.
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
t.pensize(3)# size of the pen
for i, color in enumerate(colors):
# for loop till all colors exhaust
square(t, (screen_y / 2) / 10 * (i+1), color) # determining dimensions of squares
print('Hit any key to exit')
dummy = input()
else:
popupmsg()
if __name__ == '__main__':
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.