Turtle.goto函数仅适用于两个正坐标?

问题描述

运行代码时没有错误,但是我认为问题出在turtle.goto函数上。它仅适用于两个正坐标。我似乎找不到与我接近的问题,并且我尝试了所有不同的turtle函数来建立坐标。我不确定我缺少什么或要做什么。

我知道这段代码看起来很草率,但是我是编码的新手,这只是一门大学课程。

import turtle
import time

#Asking for the shape
shape = int(input("Would you like a Square or a Circle? Type 1 for Square or 2 for Circle: "))

#Asking for the (x,y) coordinates
coordinate = int(input("Which option would you like your shape to be drawn in? please enter a number 1-4. 1. top left,2. bottom left,3. top right,4. bottom right: "))
color = int(input("What color would you like your pen to be? Input 1 for red,2 for blue,or 3 for yellow."))

#Executing if statment
if shape == 2:
    if color == 1:
        turtle.pencolor("red")
        turtle.fillcolor("blue")
    elif color == 2:
        turtle.pencolor("blue")
        turtle.fillcolor("yellow")
    else:
        turtle.pencolor("yellow")
        turtle.fillcolor("red")
        
        
#Determining pensize due to coordinate and establishing coordinate plane
        if coordinate == 1:
            turtle.penup()
            turtle.postion(-150,150)
            turtle.pendown()
            Turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()
        elif coordinate == 2:
            turtle.penup()
            turtle.setpos(-150,-150)
            turtle.pendown()
            Turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()
        elif coordinate == 3:
            turtle.penup()
            turtle.goto(150,150)
            turtle.pendown()
            turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()
        else:
            turtle.penup()
            turtle.goto(150,-150)
            turtle.pendown()
            Turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()

#Squares if statement
elif shape == 1:

    if color == 1:
        turtle.fillcolor("red")
        turtle.pencolor("blue")
    elif color == 2:
        turtle.fillcolor("blue")
        turtle.pencolor("yellow")
    else:
        turtle.fillcolor("yellow")
        turtle.pencolor("red")
        
#Determining pensize due to coordinate and establishing coordinate plane
        if coordinate == 1:
            turtle.penup()
            turtle.goto(-300,300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()
        elif coordinate == 2:
            turtle.penup()
            turtle.goto(-300,-300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()
        elif coordinate == 3:
            turtle.penup()
            turtle.goto(300,300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()
        else:
            turtle.penup()
            turtle.goto(300,-300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()

else:
    print("Your shape is not an option.")
    
    
    

解决方法

负坐标没有问题。缩进不正确。仅当您选择color = 3时才会绘制形状。

这是更新的代码:

import turtle
import time

#Asking for the shape
shape = int(input("Would you like a Square or a Circle? Type 1 for Square or 2 for Circle: "))

#Asking for the (x,y) coordinates
coordinate = int(input("Which option would you like your shape to be drawn in? please enter a number 1-4. 1. top left,2. bottom left,3. top right,4. bottom right: "))
color = int(input("What color would you like your pen to be? Input 1 for red,2 for blue,or 3 for yellow."))

#Executing if statment
if shape == 2:
    if color == 1:
        turtle.pencolor("red")
        turtle.fillcolor("blue")
    elif color == 2:
        turtle.pencolor("blue")
        turtle.fillcolor("yellow")
    else:
        turtle.pencolor("yellow")
        turtle.fillcolor("red")
        
        
#Determining pensize due to coordinate and establishing coordinate plane
    if coordinate == 1:   # shifted left to align with 'if color`
        turtle.penup()
        turtle.postion(-150,150)
        turtle.pendown()
        Turtle.pensize(3)
        turtle.begin_fill()
        turtle.circle(50)
        turtle.end_fill()
 ...........