在字符串格式的列表中循环列表
我有以下变量
BUILDING = "123"
SIDE = "ProductionA"
TODO = "traveling without moving"
我有以下清单
OS = ["Linux", "Unix", "Windows"]
我创建一个格式化的字符串列表
FLIST = [
"I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE,o),
"Other random stuff",
"Even more random stuff: ".format(TODO)]
我想循环列表:
for o in OS:
print(o)
for f in FLIST:
print(f)
我希望得到:
"I am installing in 123, side ProductionA using the Linux cd"
"Other random stuff",
"Even more random stuff: traveling without moving"
"I am installing in 123, side ProductionA using the Unix cd"
"Other random stuff",
"Even more random stuff: traveling without moving"
"I am installing in 123, side ProductionA using the Windows cd"
"Other random stuff",
"Even more random stuff: traveling without moving"
print(o)
有效,如果我在格式字符串中省略OS,我将得到值(Linux
,Unix
,Window
)。你知道吗
I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE)
但格式化列表不接受o变量,我得到的错误是:
NameError: name 'o' is not defined.
谢谢你的帮助。你知道吗
FLIST
应该是以o
作为输入的函数:尝试创建一个以o为参数的函数
FLIST
:然后使用此函数:
我已经把
FLIST
放在了循环中。试试看输出:
在行动中看到它here
相关问题 更多 >
编程相关推荐