+ 1
python issue
import sys if len(sys.argv) < 3: print("Not enough arguments.") elif len(sys.argv) > 3: print("Too many arguments.") else : r=float(sys.argv[1]) h=float(sys.argv[2]) if r < 0: print("Radius cannot be negative.") elif h < 0: print("Height cannot be negative.") else: print("The volume of the cylinder is " + '3.141592'*(r**2)*h) File "cylinder.py", line 8 h=float(sys.argv[2]) ^ IndentationError: unindent does not match any outer indentation level What's wrong here?
3 ответов
+ 5
import sys
if len(sys.argv) < 3:
print("Not enough arguments.")
elif len(sys.argv) > 3:
print("Too many arguments.")
else :
r=float(sys.argv[1])
h=float(sys.argv[2])
if r < 0:
print("Radius cannot be negative.")
elif h < 0:
print("Height cannot be negative.")
else:
print("The volume of the cylinder is " + '3.141592'*(r**2)*h)
It really matters in Python to have the same number of spaces in before the code. If the code is inside a with, if, else, for etc. statements, it needs to have extra spaces (or indent)
+ 6
Looks like an indentation error. This doesn't give an error:
import sys
if len(sys.argv) < 3:
print("Not enough arguments.")
elif len(sys.argv) > 3:
print("Too many arguments.")
else :
r=float(sys.argv[1])
h=float(sys.argv[2])
if r < 0:
print("Radius cannot be negative.")
elif h < 0:
print("Height cannot be negative.")
else:
print("The volume of the cylinder is " + '3.141592'*(r**2)*h)