如何将字符串和变量插入打印语句中?

发布于 2025-02-09 06:55:34 字数 255 浏览 2 评论 0原文

我正在尝试编程一些将显示我在打印语句中写的变量的内容,但是每当我运行代码时,它都不会显示变量,它只是说“无”。

我的代码:

sidewalkclosed = True
raining = True
xPos = 400
yPos = 400
print("Your robot is located at",print(yPos),"on the y axis and",print(xPos),"on the x 
axis.")

I am trying to program something that will display the variable that I wrote earlier in a print statement but whenever I run the code it doesn't display the variable, it just says "None".

My code:

sidewalkclosed = True
raining = True
xPos = 400
yPos = 400
print("Your robot is located at",print(yPos),"on the y axis and",print(xPos),"on the x 
axis.")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦笑流年记忆 2025-02-16 06:55:34

尝试一下。

sidewalkclosed = True
raining = True
xPos = 400
yPos = 400
print("Your robot is located at",yPos,"on the y axis and",xPos,"on the x 
axis.")
#### or ####
print(f"Your robot is located at {yPos} on the y axis and {xPos} on the x 
axis.")
# More Ways
#### or ####
print("Your robot is located at {} on the y axis and {} on the x axis.".format(yPos,xPos))

#### or ####
print("Your robot is located at %d on the y axis and %d on the x axis." % (yPos,xPos))
# Here %d (Or %i) for integer and for string type you can use %s.
# And for float you can use (%f,%e,%g) and more.

错误

,您没有得到,而不是400和400,这是因为打印功能返回无。


执行您的代码。


print("Your robot is located at",print(yPos),"on the y axis and",print(xPos),"on the x 
axis.")

从代码中输出

400
400
Your robot is located at None on the y axis and None on the x axis.

当打印函数读取他获得的输入时,在此代码中
print(ypos)print(xpos)他运行了这两个打印功能,并且
捕获返回值

Try this.

sidewalkclosed = True
raining = True
xPos = 400
yPos = 400
print("Your robot is located at",yPos,"on the y axis and",xPos,"on the x 
axis.")
#### or ####
print(f"Your robot is located at {yPos} on the y axis and {xPos} on the x 
axis.")
# More Ways
#### or ####
print("Your robot is located at {} on the y axis and {} on the x axis.".format(yPos,xPos))

#### or ####
print("Your robot is located at %d on the y axis and %d on the x axis." % (yPos,xPos))
# Here %d (Or %i) for integer and for string type you can use %s.
# And for float you can use (%f,%e,%g) and more.

ERROR

Here you got None Instead of 400 and 400 this is because the print function returns None.


Execution Of Your Code.


print("Your robot is located at",print(yPos),"on the y axis and",print(xPos),"on the x 
axis.")

OUTPUT From Your Code

400
400
Your robot is located at None on the y axis and None on the x axis.

In this code when the print function read the input he got
print(yPos) and print(xPos) He runs those two print functions and
capture the returning value Which is None.

纸短情长 2025-02-16 06:55:34

我建议使用F字符串 -

print(f"Your robot is located at {yPos} on the y axis and {xPos} on the x axis")

I would recommend using f strings -

print(f"Your robot is located at {yPos} on the y axis and {xPos} on the x axis")
熊抱啵儿 2025-02-16 06:55:34

由于某种原因,您已经将print()insral print()更改为

print("Your robot is located at",yPos,"on the y axis and",xPos,"on the x axis.")

you have put print() inside print() for some reason just change it to

print("Your robot is located at",yPos,"on the y axis and",xPos,"on the x axis.")
下雨或天晴 2025-02-16 06:55:34
sidewalkclosed = True
raining = True
xPos = 400
yPos = 400
print(f"Your robot is located at {yPos} on the y axis and {xPos} on the x axis.")
sidewalkclosed = True
raining = True
xPos = 400
yPos = 400
print(f"Your robot is located at {yPos} on the y axis and {xPos} on the x axis.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文