[New-bugs-announce] [issue38678] TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3

David Goldsmith report at bugs.python.org
Sun Nov 3 18:31:18 EST 2019


New submission from David Goldsmith <eulergaussriemann at gmail.com>:

When I run the second example code of Section 10.3 of The Python Tutorial:

import argparse
from getpass import getuser
parser = argparse.ArgumentParser(description='An argparse example.')
parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.')
parser.add_argument('--verbose', '-v', action='count')
args = parser.parse_args()
greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
print(f'{greeting}, {args.name}')
if not args.verbose:
    print('Try running this again with multiple "-v" flags!')

the greeting assignment line raises:

>>> greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
 
Is this an artifact of me being so far behind in my Python version:

Davids-MacBook-Air:~ DLG$ python3
Python 3.4.3 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:27:51) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin

Or is it a bug in the example code in the doc?

----------
assignee: docs at python
components: Documentation
messages: 355914
nosy: David Goldsmith, docs at python
priority: normal
severity: normal
status: open
title: TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3
type: crash

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38678>
_______________________________________


More information about the New-bugs-announce mailing list