+ 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ļ¼Ÿ

30th Mar 2018, 10:30 PM
Tony
3 Answers
+ 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)
31st Mar 2018, 12:03 AM
Gami
Gami - avatar
+ 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)
31st Mar 2018, 12:49 AM
David Ashton
David Ashton - avatar