You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python作业语法错误:如何在print语句中正确使用多个变量?

Python print语句嵌入多变量的正确写法

目标输出格式

Grace was planning a dream vacation to Paris.
Grace was especially looking forward to trying the local
cuisine, including stinky soup and bananas.

Grace will have to practice the language quietly to
make it easier to jump with people.

Grace has a long list of sights to see, including the
button museum and the blue park.

你的错误代码

print("Let's play Sill Sentences!")

name=str(input("Enter a name: "))
abj1=str(input("Enter an adjective: "))
abj2=str(input("Enter a different adjective: "))
food1=str(input("Enter a food: "))
food2=str(input("Enter another food: "))
noun=str(input("Enter a noun: "))
place=str(input("Enter a place: "))
verb=str(input("Enter a verb: "))
print( , name + "was planning a dream vacation to " ,place +".",name+ "was especially looking forward to trying the local cusine,including ",abj1+ ,food1+"and ",food2)

错误点梳理

  1. print( , name...)开头多了逗号,直接违反Python语法
  2. 变量和字符串拼接时缺空格,比如name + "was"会输出Gracewas,不符合格式要求
  3. abj1+ ,food1这里多了逗号,语法不合法
  4. 拼写错误:cusine应该是cuisine
  5. 你定义的abj2、noun、verb变量没用到,得对应到目标文本的剩余内容

正确写法推荐

方式1:用f-string(Python3.6+首选,简洁直观)

f-string可以直接在字符串里嵌入变量,格式清晰不易出错,还能直接保留换行结构:

print("Let's play Sill Sentences!")

# input本身返回字符串,不需要额外str()转换
name = input("Enter a name: ")
adj1 = input("Enter an adjective: ")
adj2 = input("Enter a different adjective: ")
food1 = input("Enter a food: ")
food2 = input("Enter another food: ")
noun = input("Enter a noun: ")
place = input("Enter a place: ")
verb = input("Enter a verb: ")

# 用三引号字符串一次性写出完整格式,变量用{}嵌入
output = f"""{name} was planning a dream vacation to {place}.
{name} was especially looking forward to trying the local
cuisine, including {adj1} {food1} and {food2}.

{name} will have to practice the language quietly to
make it easier to {verb} with people.

{name} has a long list of sights to see, including the
{noun} museum and the {adj2} park."""

print(output)

方式2:修正字符串拼接(基础写法)

如果不想用f-string,就修正拼接的语法错误,注意空格和标点:

print("Let's play Sill Sentences!")

name = input("Enter a name: ")
adj1 = input("Enter an adjective: ")
adj2 = input("Enter a different adjective: ")
food1 = input("Enter a food: ")
food2 = input("Enter another food: ")
noun = input("Enter a noun: ")
place = input("Enter a place: ")
verb = input("Enter a verb: ")

# 分多行print,确保格式正确
print(name + " was planning a dream vacation to " + place + ".")
print(name + " was especially looking forward to trying the local")
print("cuisine, including " + adj1 + " " + food1 + " and " + food2 + ".")
print()  # 打印空行
print(name + " will have to practice the language quietly to")
print("make it easier to " + verb + " with people.")
print()
print(name + " has a long list of sights to see, including the")
print(noun + " museum and the " + adj2 + " park.")

方式3:str.format()(兼容Python3.5及更早版本)

如果你的Python版本较低,用这种方式:

print("Let's play Sill Sentences!")

name = input("Enter a name: ")
adj1 = input("Enter an adjective: ")
adj2 = input("Enter a different adjective: ")
food1 = input("Enter a food: ")
food2 = input("Enter another food: ")
noun = input("Enter a noun: ")
place = input("Enter a place: ")
verb = input("Enter a verb: ")

output = """{} was planning a dream vacation to {}.
{} was especially looking forward to trying the local
cuisine, including {} {} and {}.

{} will have to practice the language quietly to
make it easier to {} with people.

{} has a long list of sights to see, including the
{} museum and the {} park.""".format(name, place, name, adj1, food1, food2, name, verb, name, noun, adj2)

print(output)

内容的提问来源于stack exchange,提问作者NindeBonic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 19:45:42