有没有办法在满足条件时创建中断?

问题描述 投票:0回答:1
import turtle

t = turtle

Width = int(t.numinput("Cartesain Coordinates", "Enter Width",200, minval = 200, maxval = 400))
Height = int(t.numinput("Cartesain Coordinates", "Enter Height", 200, minval = 200, maxval = 400))
Grid = t.textinput("Cartesian Coordinates", "Would you like a grid?")

# Defualt value for numinputs (x)

Default_width_value = 200

# List of possible (x) values

Possible_width_value1 = 250
Possible_width_value2 = 300
Possible_width_value3 = 350
Max_width_value = 400

# Starting the grid
if Grid == 'No':
    t.goto(0, 0)
if Grid == 'Yes':
    Grid = True
    while True:

        # The Outter Square
        t.penup()
        t.goto(-Width, +Height)
        t.pendown()
        t.goto(-Width, -Height)
        t.goto(+Width, -Height)
        t.goto(+Width, +Height)
        t.goto(-Width, +Height)

    # The Inner Lines For Width (x) portion of Grid
    # First Line
    
        t.goto(-Width, -Height)
        t.forward(50)
        t.pendown()
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)
    
    # Second Line
    
        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)
    

# Third Line

        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)

    # Fourth Line
    
        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)

    # Fifth Line

        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)

    # Sixth Line

        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)

    # Seventh Line

        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)
        break

if Width > Default_width_value < Possible_width_value1:

        # First Line

        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)

首先我想说我对 python 很陌生

问题是当为笛卡尔坐标创建网格时,我可以让它在 minval() 处创建网格,但是一旦我开始以 50 为增量增加,直到 maxval() 就是我遇到麻烦的地方。我尝试将 if() 放入 while 循环中,这就是我发现问题的地方,我也尝试了它在外面工作得很好,同时以 50 的增量增加,但我遇到的问题是它仍然会尝试即使用户拒绝网格,也会绘制网格的一部分。它是为了一项任务,我只能使用海龟模块中的内容。非常感谢任何帮助。

python loops turtle-graphics
1个回答
0
投票

首先,很高兴你学习了Python! 目前,您的代码不需要中断,因为您“手动”绘制了所有线条。
为了真正需要中断,您需要循环遍历可以在函数内组合在一起的重复代码行。 我对 Python 特定的代码做了一些调整(p.E. 导入缩写、小写变量名称):

import turtle as t

default_width_value = 200
default_height_value = 200
max_width_value = 400
max_height_value = 400  

width = int(t.numinput("Cartesain Coordinates", "Enter Width",200, minval = default_width_value, maxval = max_width_value))
height = int(t.numinput("Cartesain Coordinates", "Enter Height", 200, minval = default_height_value, maxval = max_height_value))
grid = t.textinput("Cartesian Coordinates", "Would you like a grid?")

def draw_right_line_down():
    t.right(90)
    t.forward(50)
    t.right(90)
    t.backward(-height)
    t.backward(-height)

def draw_left_line_up():
    t.left(90)
    t.forward(50)
    t.left(90)
    t.forward(+height)
    t.forward(+height)

# Starting the grid
if grid.lower() == 'no':
    t.goto(0, 0)
elif grid.lower() == 'yes':
    # grid = True  # not needed
    # The Outter Square
    t.penup()
    t.goto(-width, +height)
    t.pendown()
    t.goto(-width, -height)
    t.goto(+width, -height)
    t.goto(+width, +height)
    t.goto(-width, +height)

    # The Inner Lines For width (x) portion of grid
    # First Line

    t.goto(-width, -height)
    t.forward(50)
    t.pendown()
    t.left(90)
    t.forward(+height)
    t.forward(+height)

    number_of_lines = 1
    max_number_of_lines = 7

    while number_of_lines < max_number_of_lines:
        if number_of_lines % 2 == 1:
            draw_right_line_down()
        else:
            draw_left_line_up()
        number_of_lines += 1

    # alternative with break:
    #while number_of_lines < max_number_of_lines:
    #    draw_right_line_down()
    #    number_of_lines += 1
    #    if number_of_lines == max_number_of_lines:
    #        break
    #
    #    draw_left_line_up()
    #    number_of_lines += 1
else:
    raise ValueError("Invalid input. Please enter 'yes' or 'no'.")

希望能有所帮助,继续练习:)

© www.soinside.com 2019 - 2024. All rights reserved.