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

时间:2017-11-09 05:32:13

标签: python

所以我有一个任务,我让用户输入几个数字,并根据数字和长度输入绘制一个形状。当乌龟绘制图片时,应该在每次转弯后报告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()函数,所以任何帮助都会很棒

EDIT ---------------------------------------------- ----------------------------- 我的代码的第一部分工作得很好,但我现在有另一个问题..我必须要求用户在第二个形状上进行第二次输入(因此在绘制第一个形状并记录后,提示要求绘制第二个形状,记录)

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坐标和航向,即使只编码了一行输出。 所以你也需要帮助

4 个答案:

答案 0 :(得分:1)

如果允许您将坐标转换为整数,那么正确的格式将是(我只显示第一个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())

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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()

答案 3 :(得分:-1)

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)