Home > Net >  Indexerror of command line argument (python)
Indexerror of command line argument (python)

Time:10-03

I am new to Programming and everytime I execute this program I get the message that indexerror: list index out of range. Please fix my code

import sys

number = int(sys.argv[1])

number = number // 2

print("division:",number)

number = number ** 3

print("power:",number)

number = number   (number - 1)

print("addition:",number)

value = int(input("enter number"))

number = number   value

print("addition again:",number)

number = number * 0.123288

print("with decimal:",number)

number = int(number)

print("integer:",number)

number = chr(number)

print("character: ",number)

CodePudding user response:

TL;DR

Change the second line to be

number = int(input("Enter the number : "))

OR

Save the script with any name (example.py) and run it from the command line/cmd

>python example.py 10

Details

Actually you don't understand the logic of your code.. This script uses the sys module to return the argv list that contains arguments that user passed through the command line. To understand what do I say.. Create a new script and put this 2 lines of code..

import sys
print(sys.argv)

Now don't run the script directly, but save it as test.py for example and run it from the command line/cmd

python test.py arg1 arg2 argN...

Certainly your command line/cmd points to the script directory and id you typed ls or dir you will see your script..

Now try those commands

>python test.py hello world
['test.py', 'hello', 'world']

>python test.py 123
['test.py', '123']

>python test.py
['test.py']

I think you got it and knew, why do we use the argv list, it does contain the arguments passed from the command line(only), and the first element in the list usually the script name/path.

Now let us back to your code..

import sys
number = int(sys.argv[1])
number = number // 2
print("division:",number)
number = number ** 3
print("power:",number)
number = number   (number - 1)
print("addition:",number)
value = int(input("enter number"))
number = number   value
print("addition again:",number)
number = number * 0.123288
print("with decimal:",number)
number = int(number)
print("integer:",number)
number = chr(number)
print("character: ",number)

You will find this 2nd line number = int(sys.argv[1]) It reads the arg with index 1 and convert it to int.

That's good if you run the script from the command line like that

 >python example.py 10
division: 5
power: 125
addition: 249
enter number5
addition again: 254
with decimal: 31.315151999999998
integer: 31
character:  

But, when you run the script directly from your python ide, you can't pass arguments to the argv list, and it contains only one element at index zero (the script name).

So, when you want to get the element at index 1 sys.argv[1], it will throw an exception you do know well.

number = int(sys.argv[1])
IndexError: list index out of range

Therefore,

  • Run your script from the command line or
  • read the number value using the input function, in that case you can run your script directly and it will work as this example.
import sys

number = int(input("Enter the number : "))
number = number // 2
print("division:",number)
number = number ** 3
print("power:",number)
number = number   (number - 1)
print("addition:",number)
value = int(input("enter number : "))
number = number   value
print("addition again:",number)
number = number * 0.123288
print("with decimal:",number)
number = int(number)
print("integer:",number)
number = chr(number)
print("character: ",number)

CodePudding user response:

Save this code in the file with py extension

number = int(input("Please enter number: "))

number = number // 2
print("division:", number)

number = number ** 3
print("power:", number)

number = number   (number - 1)
print("addition:", number)

value = int(input("enter number"))
number = number   value
print("addition again:", number)

number = number * 0.123288
print("with decimal:", number)

number = int(number)
print("integer:", number)

number = chr(number)
print("character: ", number)

Example - I stored this code in test.py

Now run Program

python3 test.py

CodePudding user response:

First, you are trying to convert to an integer the following :

number = int(sys.argv[1])

It cannot work because argv[1] represents the first command-line argument (as a string) supplied to the script in question.

For instance, in a python notebook, the list sys.argv should be composed of the following strings :

['self.py', 'arg1', 'arg2']
  • Related