Solution: Calculator (argv)
import sys
def main():
if len(sys.argv) < 4:
exit("Usage: " + sys.argv[0] + " OPERAND OPERATOR OPERAND")
a = float(sys.argv[1])
b = float(sys.argv[3])
op = sys.argv[2]
if op == '+':
res = a + b
elif op == '-':
res = a - b
elif op == '*':
res = a * b
elif op == '/':
res = a / b
else:
print("Invalid operator: '{}'".format(op))
exit()
print(res)
main()
The multiplication probably won't work because the Unix/Linux shell replaces the * by the list of files in your current directory and thus the python script will see a list of files instead of the *
.
This is not your fault as a programmer. It is a user error. The correct way to run the script is python calc.py 2 '*' 3
.