Python 传递函数,参数来自格式化打印语句,参数来自{}

Python 传递函数,参数来自格式化打印语句,参数来自{},python,python-3.x,Python,Python 3.x,嗨,r/python程序员 我试图在Python3中的格式化打印语句中输入一个函数。该函数具有我希望从格式化打印{}中获取的参数。但一旦这样做,它就没有通过。如何做到这一点 代码#1>错误处理函数 # >> function :: error pop# msg = error message , shutdown = True or Falsedef pop_error(msg, shutdown):if shutdown == True:print(f"[{c_red}{

嗨,r/python程序员

我试图在Python3中的格式化打印语句中输入一个函数。该函数具有我希望从格式化打印{}中获取的参数。但一旦这样做,它就没有通过。如何做到这一点

代码#1>错误处理函数

# >> function :: error pop# msg = error message , shutdown = True or Falsedef pop_error(msg, shutdown):if shutdown == True:print(f"[{c_red}{msg}{c_white}] Exiting..")exit()elif shutdown == False:print(f"[{c_red}{msg}{c_white}]")
代码#2>调用错误处理程序

# global variablesglobal engine_argglobal async_arg# check selected engineif engine_arg == 'ddg':print("Starting DDG")elif engine_arg == 'pbin':print("Starting Pastebin")else:#pop_error("Invlaid Engine {?engine_arg?}", True)print(f"{pop_error('Invalid Engine {engine_arg}', True)}")exit()
编辑:主要问题是我无法在函数的第一个参数的双引号中添加局部变量。这就是为什么我使用格式化字符串,这样我就可以使用“{variable}”,但是在函数的引号中,这似乎是不可能的,尽管使用了print(f)

已解决:
print(f“{pop_error(f'Invalid Engine{Engine_arg}',True)}”


图像引用:

不要使用
f-string
,只需使用
print(pop_error(arg1,arg2))
我尝试过,但我也想包括局部变量..print(pop_error(“无效引擎:?”,True))代替“?”,这就是为什么我想使用格式化字符串,以便使用{variable}在双引号内..外部的f字符串似乎毫无意义,请尝试
print(pop_error(f'Invalid Engine{Engine_arg}',True))
谢谢你的工作!你真是天才。为什么我从来没有想过这个!!我们可以将整个语句放在{}内..:D
>>> itworks = 'wow it worked'
>>> f'''{f'{itworks}'}'''
>>> 'wow it worked'

# in you case


print(f"{pop_error(f'Invalid Engine {engine_arg}', True)}"