格式字符串错误的参数太多(代码为**)

2024-09-23 22:29:43 发布

您现在位置:Python中文网/ 问答频道 /正文

正如你所看到的,我正在努力使乒乓球,问题是球员A或“nameA”得分保持为“{}”任何修复?如果有人知道到底是什么问题,以及我将来如何避免,请告诉我。“代码”之间的代码需要修复

import turtle
nameA = input("Player A Enter Your Name!")
nameB = input("Player B Enter Your Name!")

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write(nameA + ": 0 " + nameB + ": 0 ", align="center", font=("Courier", 24, "normal"))

# Main game loop
while True:
wn.update()

#Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)

#Border checking
if ball.ycor() > 290:
    ball.sety(290)
    ball.dy *= -1

if ball.ycor() < -280:
    ball.sety(-280)
    ball.dy *= -1

if ball.xcor() > 380:
    ball.goto(0, 0)
    ball.dx *= -1
    score_b += 1
    pen.clear()
    **pen.write(nameA +": {} " + nameB+": {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))**

if ball.xcor() < -390:
    ball.goto(0, 0)
    ball.dx *= -1
    score_a += 1
    pen.clear()
    **pen.write(nameA +": {} " + nameB+": {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))**

    # Paddle and ball collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() -50):
    ball.setx(340)
    ball.dx *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() -50):
    ball.setx(-340)
    ball.dx *= -1

Tags: andifwritescorecenteraligngotoball
2条回答

所讨论的表达是

": {}".format(score_a, score_b)

您有一个只有一个格式槽的字符串,但您试图在其中强制两个变量

函数调用绑定比字符串连接更紧密

您可以添加f'字符串。像这样:f'{nameA} : {score_a} {nameB}: {score_b}'

像这样:

import turtle
nameA = input("Player A Enter Your Name!")
nameB = input("Player B Enter Your Name!")

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write(nameA + ": 0 " + nameB + ": 0 ", align="center", font=("Courier", 24, "normal"))

# Main game loop
while True:
    wn.update()

#Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)

#Border checking
if ball.ycor() > 290:
    ball.sety(290)
    ball.dy *= -1

if ball.ycor() < -280:
    ball.sety(-280)
    ball.dy *= -1

if ball.xcor() > 380:
    ball.goto(0, 0)
    ball.dx *= -1
    score_b += 1
    pen.clear()
    pen.write(f'{nameA} : {} {score_a} {nameB} : {score_b}', align="center", font=("Courier", 24, "normal"))

if ball.xcor() < -390:
    ball.goto(0, 0)
    ball.dx *= -1
    score_a += 1
    pen.clear()
    pen.write(f'{nameA} : {score_a} {nameB} : {score_b}', align="center", font=("Courier", 24, "normal"))

    # Paddle and ball collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() -50):
    ball.setx(340)
    ball.dx *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() -50):
    ball.setx(-340)
    ball.dx *= -1

相关问题 更多 >