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



所以我有一个赋值,让用户输入几个数字,它根据输入的数字和长度绘制一个形状。当乌龟画画时,它应该报告每个转弯后的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-规范迷你语言

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

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)

最新更新