繁体   English   中英

尝试使用 .format() 函数格式化 x 和 y 坐标

[英]trying to format x and y coordinates using .format() function

所以我有一个作业,我让用户输入几个数字,然后根据输入的数字和长度绘制一个形状。 当海龟画图时,它应该在每转一圈后报告 X 和 Y 坐标和航向; 但是 X 和 Y 坐标需要格式化为整数,以便它们显示整数而不是小数。 我应该使用 .format() 函数来这样做,但我不知道在哪里以及如何使用它。

到目前为止,这是我的代码:

import turtle
lawrence = tur  tle.Turtle()
lawrence.goto(-150,0)
lawrence.pencolor('white')
lawrence.speed(2)

#setup of window that final image will be displayed on
window = turtle.Screen()
window.setup(1000,1000)
window.title("Homework 3")
window.bgcolor('black')


user_shape = int(input('What do you want me to draw? (1 = square, 2 = triangle): '))
if user_shape == 1:
print('I will draw a square')
else:
print('I will draw an equilateral triangle')

user_length = int(input('How long do you want the sides of your square to be? Please enter the number of pixels (e.g. 100): '))

for num in range(user_shape):
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 1st corner is at: ',lawrence.xcor().format(),',',lawrence.ycor(), 'and my heading is',lawrence.heading())
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 2nd corner is at: ',lawrence.xcor(),',',lawrence.ycor(), 'and my heading is',lawrence.heading())
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 3rd corner is at: ',lawrence.xcor(),',',lawrence.ycor(), 'and my heading is',lawrence.heading())
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 4th corner is at: ',lawrence.xcor(),',',lawrence.ycor(), 'and my heading is',lawrence.heading())

window.exitonclick()

我只是在一个基本的 python 编程类中,只需要知道如何使用 .format() 函数以及在哪里所以任何帮助都会很棒

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - -------------------------- 我的代码的第一部分效果很好,但我现在有另一个问题..我必须向用户询问第二个形状的第二个输入(因此在绘制并记录第一个形状后,提示要求绘制和记录第二个形状)

import turtle
lawrence = turtle.Turtle()
lawrence.goto(-150,0)
lawrence.pencolor('white')
lawrence.speed(2)

#setup of window that final image will be displayed on
window = turtle.Screen()
window.setup(1000,1000)
window.title("Homework 3")
window.bgcolor('black')


user_shape = int(input('What do you want me to draw? (1 = square, 2 = triangle): '))
if user_shape == 1:
    print('I will draw a square')
    user_length = int(input('How long do you want the sides of your square to be? Please enter the number of pixels (e.g. 100): '))
    for num in range(user_shape):
        lawrence.forward(user_length)
        lawrence.left(90)
        print('My 1st corner is at: ','{0:.0f}'.format(lawrence.xcor()),',','{0:.0f}'.format(lawrence.ycor()),'and my heading is',lawrence.heading())
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 2nd corner is at: ','{0:.0f}'.format(lawrence.xcor()),',','{0:.0f}'.format(lawrence.ycor()),'and my heading is',lawrence.heading())
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 3rd corner is at: ','{0:.0f}'.format(lawrence.xcor()),',','{0:.0f}'.format(lawrence.ycor()),'and my heading is',lawrence.heading())
    lawrence.forward(user_length)
    lawrence.left(90)
    print('My 4th corner is at: ','{0:.0f}'.format(lawrence.xcor()),',','{0:.0f}'.format(lawrence.ycor()),'and my heading is',lawrence.heading())
if user_shape == 2:
    print('I will draw an equilateral triangle')
    user_length = int(input('How long do you want the sides of your triangle to be? Please enter the number of pixels (e.g. 100): '))
    for num in range(user_shape):
        lawrence.forward(user_length)
        lawrence.left(135)
        print('My 1st corner is at: ','{0:.0f}'.format(lawrence.xcor()),',','{0:.0f}'.format(lawrence.ycor()),'and my heading is',lawrence.heading())

window.exitonclick()

所以我遇到的问题是,即使只有一个输出,程序也会报告 2 个坐标,我发现当你按 2 表示你想要绘制一个三角形,然后指出程序的边长时从用户那里获取“2”输入并将其乘以它需要报告的坐标数。 因此,与其画一条线并报告 x,y 坐标和航向,它报告 x,y 坐标和航向,然后是第二组 x,y 坐标和航向,即使只有一行输出被编码。 所以你也需要帮助

如果允许您将坐标转换为整数,那么正确的格式将是(我只显示第一个print语句):

print('My 1st corner is at: {:d},{:d} and my heading is {}'.format(int(lawrence.xcor()), int(lawrence.ycor()), lawrence.heading())

或者,如果您需要舍入浮点数,请将int(lawrence.xcor())替换为int(round(lawrence.xcor()))

如果不允许将坐标转换为整数,则使用以下格式:

print('My 1st corner is at: {:.0f},{:.0f} and my heading is {}'.format(lawrence.xcor(), lawrence.ycor(), lawrence.heading())

lawrence.xcor().format()替换为'{0:.0f}'.format(lawrence.xcor())

字符串格式是一种非常强大的工具,可以以各种形式输出文本。 如果您的目标是编写简单易读的代码,我建议您通读本章(我总是自己回去阅读): https : //docs.python.org/3.6/library/string.html#format-specification-mini-语

现在你的问题:如果你要重复使用相同的模式,你可以先定义字符串(循环外),例如:

p_string = 'My {} corner is at: {:.0f},{:.0f} and my heading is {}'

您只需将未知的{}放在要插入字符串的位置。 现在您可以继续打印:

print(p_string.format("1st",*lawrence.pos(), lawrence.heading()))

...

print(p_string.format("2nd",*lawrence.pos(), lawrence.heading()))

lawrence.pos()之前的*解包元组/列表(意味着它相当于: lawrence.xcor(), lawrence.ycor()

print('My 1st corner is at: ',lawrence.xcor().format(),',',lawrence.ycor(), 'and my heading is',lawrence.heading())

print_str = 'My 1st corner is at: ({}, {}), and my heading is {}'.format(lawrence.xcor(), lawrence.ycor(), lawrence.heading())
print(print_str)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM