简体   繁体   中英

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

So I have an assignment where I have the user input a few numbers and it draws a shape based on the number and length input. As the turtle draws the picture it is supposed to report the X and Y coordinates and heading after each turn; however the X and Y coordinates need to be formatted as integers so that they display a whole number instead of a decimal. I'm supposed to use the .format() function to do so but I have no idea where and how to use it.

Here is my code so far:

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

I'm only in a basic python programming class and just need to know how to use the .format() function and where so any help would be great

EDIT--------------------------------------------------------------------------- that first part of my code works great but I have another problem now.. I have to ask the user for a second input on a second shape ( so after the first shape is drawn and recorded the prompt asks for a second shape to draw and record)

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

so the issue that i'm having is that the program reports 2 coordinates even with only one output and I've discovered that when you press 2 to indicate you want a triangle to be drawn and then indicate the length of the sides the program is taking the '2' input from the user and multiplying it by the number of coordinates it needs to report. so instead of drawing out a line and reporting x,y coordinates and heading it reports x,y coordinated and heading and then a second set of x,y coordinates and heading even though only one line of outputs is coded. so ya need help there too

If you are allowed to convert your coordinates to integer numbers then correct formatting would be (I am showing only the first print statement):

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

or if you need to round floating point numbers, replace int(lawrence.xcor()) with int(round(lawrence.xcor())) .

If you are not allowed to convert coordinates to integers, then use the following formatting:

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

String format is a very powerful tool to output text in various forms. If you are aiming to write simple and readable code I recommend reading through this chapter (I always go back to it myself): https://docs.python.org/3.6/library/string.html#format-specification-mini-language

Now to your question: if you are going to use the same pattern repeatedly you can first define the string (outside the loop), eg:

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

You simply put the unkown {} where you want to insert a string. Now you can go ahead and print:

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

...

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

The * before lawrence.pos() unpacks the tuple/list (meaning it is equivalent to: lawrence.xcor(), lawrence.ycor() )

from

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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