gpt4 book ai didi

python - 使用变量的每行报价错误

转载 作者:行者123 更新时间:2023-12-01 07:00:59 25 4
gpt4 key购买 nike

我试图获得一个标题来一次打印整个文本墙,并且我需要变量与文本墙一起显示。我已经定义了所有变量并使用 .format 将它们放入字符串中。除了第一行之外,我在每个引用中都遇到语法错误。如果这有什么区别的话,我正在使用 Idle

我尝试将变量放在字符串之外并用逗号分隔,但它仍然给出相同的错误

print("# Activity {0} Task {1}".format(activity, task)
"# File: {0}".format(file)
"# Date: {0}".format(date)
"# By: {0}".format(name)
"# Section: {0}".format(section)
"# Team: {0}".format(team)
"# "
"# ELECTRONIC SIGNATURE"
"# {0}".format(name)
"# "
"# The electronic signature above indicates the script"
"# submitted for evaluation is my individual work, and I"
"# have a general understanding of all aspects of its"
"# development and execution."
"# "
"# {0}".format(desciption)

第二行最后一个引号出现语法错误。

最佳答案

如果您运行的是 python 3.6+,那么您可以使用f-前缀来直接在字符串中连接值。它更加简单和干净。更多信息here .

示例:

activity, task, file, date, name = 'a', 'b', 'c', 'd', 'e' 

result = f'''# Activity {activity} Task {task}
# File: {file}
# Date: {date}
# By: {name}'''

print(result)

输出:

# Activity a Task b
# File: c
# Date: d
# By: e

关于python - 使用变量的每行报价错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58630826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com