Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 IndexError: List index out of range issue
#1
Hi, could someone help me on this code plz ? It says"IndexError: List index out of range issue" in "n = int(sys.argv[1])". btw i m using python 3.7 if it can help.

import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

def multiplication_table(n, N=None, number_labels=True):
    """Create and plot an image of a multiplication table modulo n

    The table is of ij % n for i, j = 1, 2, ..., N-1. If not supplied,
    N defaults to n. If N is a mutiple of n, the pattern is repeated
    across the created image. The "rainbow" colormap is used, but zeros
    (corresponding to factors of n) are displayed in white.

    """

    if not N:
        N=n

    # A multiplication table (modulo n)
    arr = np.fromfunction(lambda i,j:(i+1)*(j+1) % n, (N-1,N-1))

    # Select a colormap, but we'll set 0 values to white
    cmap = matplotlib.cm.get_cmap('rainbow')
    cmap.set_under('w')

    fig, ax = plt.subplots()
    # Plot an image of the multiplication table in colours for values greater
    # than 1. Zero values get plotted in white thanks to set_under, above.
    ax.imshow(arr, interpolation='nearest', cmap=cmap, vmin=1)

    # Make sure the tick marks are correct (start at 1)
    tick_formatter = FuncFormatter(lambda v, pos: str(int(v+1)))
    ax.xaxis.set_major_formatter(tick_formatter)
    ax.yaxis.set_major_formatter(tick_formatter)

    # For small n, write the value in each box of the array image.
    if number_labels and N < 21:
        for i in range(N-1):
            for j in range(N-1):
                ax.annotate(s=str((i+1)*(j+1)%n), xy=(i,j), ha='center',
                            va='center')

# The user supplies n (and optionally N) as command line arguments
n = int(sys.argv[1])
try:
    N = int(sys.argv[2])
except IndexError:
    N = None

multiplication_table(n, N, False)
plt.show()
Quote
#2
Well, that depends on how you call it. You have to provide at least one arguments to that script when it is called from the command line. Otherwise you will get that error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 17 427 Jan-06-2020, 05:56 AM
Last Post: KiNeMs
  IndexError: index 8 is out of bounds for axis 0 with size 8 Help_me_Please 6 354 Jan-03-2020, 01:51 PM
Last Post: Help_me_Please
  For List Loop Issue Galdain 2 106 Dec-31-2019, 04:53 AM
Last Post: Galdain
  How to find something in a list using its index rix 1 98 Dec-20-2019, 04:12 PM
Last Post: stullis
  item = index position - list of list RavCOder 9 250 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  IndexError: list index out of range bug? CaptainCsaba 4 807 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba
  List/String seperation issue YoungGrassHopper 13 453 Sep-20-2019, 11:57 AM
Last Post: perfringo
  returns index of list if contains a word zarize 0 181 Sep-09-2019, 09:29 AM
Last Post: zarize
  MAWS.py PRGM:IndexError: list index out of range bcramer 0 189 Aug-28-2019, 04:45 PM
Last Post: bcramer
  Select a part of an element of a list with Index BollerwagenIng 0 340 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng

Forum Jump:


Users browsing this thread: 1 Guest(s)