簡體   English   中英

我如何解決海龜中奇數的問題,Python

[英]How do i solve problem in odd number in turtle, Python

height = input("Enter the height: ")
height = int(height)
width = input("Enter the width: ")
width = int(width)

import turtle

width = width - 1

turtle.speed(1)

turtle.penup()

for y in range(height // 2):
    for x in range(width):
        turtle.dot()
        turtle.forward(20)
        turtle.dot()
    turtle.right(90)
    turtle.forward(20)
    turtle.right(90)
    for x in range(width):
        turtle.dot()
        turtle.forward(20)
        turtle.dot()
    turtle.left(90)
    turtle.forward(20)
    turtle.left(90)
turtle.exitonclick()

在此處輸入圖像描述

我想在 python 圖形海龜中打印點。 對於我的寬度,它作為我的輸入給出。 但是對於高度,如果數字是偶數,我會得到准確的 output 但如果數字是奇數,我會得到高度 - 1。我知道我的代碼、邏輯不是高效和准確的。 我是一個自學者(僅限書籍)。

與其修補嵌套循環,不如簡化它們:

import turtle

height = int(input("Enter the height: "))
width = int(input("Enter the width: "))

turtle.speed('slowest')
turtle.penup()

angle = 90

for _ in range(height):
    for _ in range(width - 1):
        turtle.dot()
        turtle.forward(20)

    turtle.dot()

    turtle.right(angle)
    turtle.forward(20)
    turtle.right(angle)

    angle *= -1

turtle.hideturtle()
turtle.exitonclick()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM