Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串参数传递给函数-为什么不';它不起作用吗?_Python_String_Function - Fatal编程技术网

Python 将字符串参数传递给函数-为什么不';它不起作用吗?

Python 将字符串参数传递给函数-为什么不';它不起作用吗?,python,string,function,Python,String,Function,尝试将字符串参数传递给函数,然后函数将在整个函数中用作变量。出于某种原因,当我尝试这样做时,它不起作用。我做错了什么 import subprocess def printerSetup(printer): subprocess.call(r'Cscript c:/windows/System32/Printing_Admin_Scripts/en-US/Prnport.vbs -a -r "'printer'.print.web.com" -h "' + printer + '.pri

尝试将字符串参数传递给函数,然后函数将在整个函数中用作变量。出于某种原因,当我尝试这样做时,它不起作用。我做错了什么

import subprocess
def printerSetup(printer):
    subprocess.call(r'Cscript c:/windows/System32/Printing_Admin_Scripts/en-US/Prnport.vbs -a -r "'printer'.print.web.com" -h "' + printer + '.print.web.com" -o raw')
    if printer == 'saturn' or printer == 'jupiter' or printer == 'neptune':
        subprocess.call(r'rundll32 printui.dll, PrintUIEntry /if /b "' + printer + '" /f w:\printers\toshibae3511\eng\est_c2.inf /r "' + printer + '.print.web.com" /m "TOSHIBA e-STUDIO Color PS3"')
    if printer == 'mercury':
        subprocess.call(r'rundll32 printui.dll, PrintUIEntry /if /b "' + printer + '" /f w:\printers\dell1720\drivers\print\dell1720\DKABJ740.inf /r "' + printer + '.print.web.com" /m "Dell Laser Printer 1720dn"')

printerSetup("neptune")
printerSetup("mercury")
编辑了这个节目。尝试运行此新程序后,出现以下错误:

C:\Python27\Projects\Printer Setup>c:\python27\python.exe saturn.py
  File "saturn.py", line 3
    subprocess.call(r'Cscript c:/windows/System32/Printing_Admin_Scripts/en-US/P
rnport.vbs -a -r "'printer'.print.web.com" -h "' + printer + '.print.web.c
om" -o raw')

                         ^
SyntaxError: invalid syntax

您需要为每个
语句指定
变量==value
,如下所示:

if printer == 'saturn' or printer == 'jupiter' or printer == 'neptune':
您还忘记了每个
if
语句的尾随冒号

如果您想说“此变量是否与此值列表匹配?”,则以下内容可能更适合您:

if printer in ('saturn', 'jupiter', 'neptune'):
您还需要将变量添加到字符串中-您不能将它们相邻放置:

'string' + variable + 'string'

 # not

 'string'variable'string'

请指定它以何种方式不起作用。您是否试图将
printer
参数的值放入第一个
subprocess.call()
行的参数中?它在什么意义上不起作用?在if语句之后需要冒号。
if printer=='saturn'或'jupiter'或'neptune'
您忘记了一些
printer==
,就我所想的,“string”变量“string”应该将变量插入字符串中,但我必须使用+,现在不是。您可以通过这种方式将字符串添加到一起,但不能使用变量
“string”“string”