简体   繁体   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()

在此处输入图像描述

I want to print dots in python graphical turtle.我想在 python 图形海龟中打印点。 For my width, it's giving out as my input.对于我的宽度,它作为我的输入给出。 But for the height, if the number is even I am getting the accurate output but if the number is odd I am getting height - 1. I know that my code, logic are not efficient & accurate.但是对于高度,如果数字是偶数,我会得到准确的 output 但如果数字是奇数,我会得到高度 - 1。我知道我的代码、逻辑不是高效和准确的。 I am a self-learner (by books only).我是一个自学者(仅限书籍)。

Rather than patch your nested loops, let's simplify them:与其修补嵌套循环,不如简化它们:

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