Python Forum
Argparse error when inputting values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Argparse error when inputting values
#1
I'm trying to resolve the errors in this code. We are trying to use argparse, and I get to num_nums = len(args.n) and I get errors. The error gets generated when reading the next line and reads as:

ERROR CODE
Error:
print("Adding %d numbers." % num_nums) TypeError: %d format: a number is required, not NoneType
I can't figure out what is failing with the %d argument that is being passed.
I am looking for assistance with the code to get it to a working state. Full code is:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-n", help="Number to add")
parser.add_argument("--indent", help="Indent output")

args = parser.parse_args()

num_nums = (args.n)
print("Adding %d numbers." % num_nums)
sum = 0
for n in range(0, num_nums):
    sum = sum + args.n[n]

if (args.i):
    print("\tSum = %d" % sum)
else:
    print("Sum = %d" % sum)
Reply
#2
First, put your code inside python tags so it is easily readable (and so indentation can be seen).

Second, how are you running it? Are you giving an argument to -n? When I do I get a str error instead of a NoneType error.

You haven't told argparse that -n is required, so it will let you run without the argument. Also, the default is to read the argument as a string. So num_nums is a string and the print won't work when you ask it to format a string with %d.

You can modify the add_argument to resolve both. Something like

parser.add_argument("-n", type=int, required=True, help="Number to add")
Reply
#3
as a side note, you are using old-style string formatting, there is str.format() method and in 3.6+ - f-strings. better use f-strings.
also sum is built-in function. don't use it as name/variable.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 709 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 1,687 May-11-2022, 07:12 AM
Last Post: normanwolf
  Not able to figure out what is wrong with argparse radioactive9 31 8,625 Mar-16-2022, 07:50 PM
Last Post: deanhystad
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,715 Jan-28-2021, 05:22 PM
Last Post: snippsat
  argparse --help in one line. Denial 1 2,007 Sep-20-2020, 03:38 PM
Last Post: deanhystad
  Why this pycharm warning for argparse formatter_class value? pjfarley3 2 2,144 Sep-09-2020, 05:23 AM
Last Post: pjfarley3
  Can argparse support undocumented options? pjfarley3 3 2,228 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,604 Jul-31-2020, 02:01 PM
Last Post: pjfarley3
  Error: too many values to unpack Mike 1 5,145 Oct-30-2019, 03:07 PM
Last Post: buran
  Letters with Accents Not Inputting Phylus 0 1,467 Jun-12-2019, 04:05 PM
Last Post: Phylus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020