0

I've got this code.

#!/usr/bin/python

from optparse import OptionParser   #import the OptionParser object from this module

parser = OptionParser()

parser.add_option("-f", "--first", dest="meal", help="prix repas", type="float")  
parser.add_option("-s", "--second", dest="tip", help="le tip", type="float")
parser.add_option("-t", "--third", dest="tax", help="tax", type="float")


(options, args) = parser.parse_args() 

tax_value = options.meal * options.tax
meal_with_tax = tax_value + options.meal
tip_value = meal_with_tax * tip

if not (options.meal and options.tip): 
parser.error("You need to supply an argument for -s")

print "le prix du repas est '{}'.".format(options.meal)
print "Le tip est de '{}'.".format(options.tip)
print "Le tip est de '{}'.".format(options.tip)

Each time I'm running it with the following command line

./tip_re1_arg.py -s 5 2 3

I've got this error tax_value = options.meal * options.tax TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'

Why? It seems that I've done everything right in term of type. Or did I?

Sorry, I'm very much of a beginner in Python.

Andy K
  • 4,944
  • 10
  • 53
  • 82
  • 1
    Aside: if you're writing new code, you should probably use [`argparse`](https://docs.python.org/2.7/library/argparse.html) and not `optparse` (reasons [here](http://stackoverflow.com/questions/3217673/why-use-argparse-rather-than-optparse)). – DSM Sep 02 '14 at 17:35
  • It would be interesting to hear how you thought `-s 5 2 3` would be handled by your code. Which of 5, 2, and 3 should be the value for `meal`, and which for `tax`? – chepner Sep 02 '14 at 17:39
  • Hi Chepner, I thought that `optparse` would react like a `sys.argv`. Infortunately, it is not the case. – Andy K Sep 03 '14 at 08:31
  • I got this exercise here https://docs.google.com/document/d/1woHNUHfK3YfDZeYwx3wNYmtNZAXAgT82_QeWhFvz3gA/edit#heading=h.893cxoproj3e – Andy K Sep 03 '14 at 08:52

2 Answers2

3

You have explicitly configured the parser to expect the parameters as options:

./tip_re1_arg.py -f 5 -s 2 -t 3

./tip_re1_arg.py --first 5 --second 2 --third 3

Currently your input arguments end up in args.

bereal
  • 32,519
  • 6
  • 58
  • 104
  • Hi Bereal, your answer is more complete but `user3038802`was the quickest. Many thanks for showing me what can be done especially with the `--first`. – Andy K Sep 03 '14 at 08:33
  • Btw and I have to share, my first python question that did not have minus! Hurrah !!! – Andy K Sep 03 '14 at 13:27
0

You need to specify the options when you run it. In other words:

./tip_re1_arg.py -f 5 -s 2 -t 3

The parser will not just take 3 undeclared options and assign them. Now, you could use them without the tags by using the input argument array directly (with sys.argv[1] etc). This is probably less user friendly though, than changing your arguments to what they actually mean (ie -m/--meal, -x/--tax, -t/--tip).

Anyway, the way you are calling it, meal and tax are not actually assigned and so are empty, giving you the NoneType error.

HamHamJ
  • 435
  • 2
  • 10