import sys
if len(sys.argv) < 3:
print('Not enough arguments')
elif len(sys.argv) > 3:
print('Too many arguments')
radius = float(sys.argv[1])
height = float(sys.argv[2])
PI = 3.141592
v = PI*radius*radius*height
else:
if radius < 0 :
print('Radius cannot be negative')
elif radius > 0 and height < 0 :
print('Height cannot be negative')
else:
print(int(v))
我一直在用“else:”获得语法错误。代码有什么问题吗?
答案 0 :(得分:2)
您的代码存在indentation
问题。我也改变你的逻辑如下。您只想在满足所有条件时计算v
:
import sys
if len(sys.argv) < 3:
print('Not enough arguments')
elif len(sys.argv) > 3:
print('Too many arguments')
else:
radius = float(sys.argv[1])
height = float(sys.argv[2])
if radius < 0 :
print('Radius cannot be negative')
elif radius > 0 and height < 0 :
print('Height cannot be negative')
else:
PI = 3.141592
v = PI*radius*radius*height
print(int(v))
祝你好运!
答案 1 :(得分:1)
我认为这就是你要做的事。
尝试使用此代码: -
import sys
if len(sys.argv) < 3:
print('Not enough arguments')
elif len(sys.argv) > 3:
print('Too many arguments')
else:
radius = float(sys.argv[1])
height = float(sys.argv[2])
if radius < 0 :
print('Radius cannot be negative')
elif radius > 0 and height < 0 :
print('Height cannot be negative')
else:
PI = 3.141592
v = PI*radius*radius*height
print(int(v))
<强>输出强>
$ python3 test200.py 25 10
19634
$ python3 test200.py 2 10
125
$ python3 test200.py 2 -10
Height cannot be negative
$ python3 test200.py -2 10
Radius cannot be negative