Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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/2/cmake/2.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 len(sys.argv)中的语法错误_Python - Fatal编程技术网

Python len(sys.argv)中的语法错误

Python len(sys.argv)中的语法错误,python,Python,Python解释器在运行以下代码时出现语法错误: import sys if len(sys.argv) == 3: a=sys.argv[1] b=sys.argv[2] sum=int(a) + int(b) print "The sum is: ", sum elif len(sys.argv) != 3: print "Only two arguments allowed !" else: print "Please enter two numbers as a

Python解释器在运行以下代码时出现语法错误:

import sys

if len(sys.argv) == 3:
  a=sys.argv[1]
  b=sys.argv[2]
  sum=int(a) + int(b)
  print "The sum is: ", sum
elif len(sys.argv) != 3:
  print "Only two arguments allowed !"
else:
  print "Please enter two numbers as argument with the script. Try again !"
错误:

luckee@zarvis:~/python$ ./sumtwo.py 5 10
./sumtwo.py: line 3: syntax error near unexpected token `sys.argv'
./sumtwo.py: line 3: `if len(sys.argv) == 3:'

您的文件由shell解释,而不是Python解释器。运行二进制文件时,请尝试指定它:

luckee@zarvis:~/python$ python sumtwo.py 5 10
或者,您可以添加脚本的第一行

#!/usr/bin/python
import sys

if len(sys.argv) == 3:
  a=sys.argv[1]
  b=sys.argv[2]
  sum=int(a) + int(b)
  print "The sum is: ", sum
elif len(sys.argv) != 3:
  print "Only two arguments allowed !"
else:
  print "Please enter two numbers as argument with the script. Try again !"

您的文件由shell解释,而不是Python解释器。运行二进制文件时,请尝试指定它:

luckee@zarvis:~/python$ python sumtwo.py 5 10
或者,您可以添加脚本的第一行

#!/usr/bin/python
import sys

if len(sys.argv) == 3:
  a=sys.argv[1]
  b=sys.argv[2]
  sum=int(a) + int(b)
  print "The sum is: ", sum
elif len(sys.argv) != 3:
  print "Only two arguments allowed !"
else:
  print "Please enter two numbers as argument with the script. Try again !"

您的文件由shell解释,而不是Python解释器。尝试
pythonsumtwo.py
或添加
#!python
作为您文件的第一个文件。Rogalski已经找到了它。这也有点相切,但是您的
if
elif
子句涵盖了所有可能的-len(sys.argv)必须是==3或!=3.else永远不会被触发。您的文件由shell而不是Python解释器解释。尝试
pythonsumtwo.py
或添加
#!python
作为您文件的第一个文件。Rogalski已经找到了它。这也有点相切,但是您的
if
elif
子句涵盖了所有可能的-len(sys.argv)必须是==3或!=3.否则就永远不会被触发。