Python Forum
IndexError: List index out of range issue
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()
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  removing one list element without using its index paul18fr 7 3,079 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  IndexError: index 31 is out of bounds for axis 0 with size 31 YL789 1 1,726 Sep-21-2024, 09:46 AM
Last Post: Gribouillis
  List Comprehension Issue johnywhy 5 3,088 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 16,121 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 17,456 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 16,620 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 4,479 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  Python List Issue Aggie64 5 3,569 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  I'm getting a String index out of range error debian77 7 5,203 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 2,800 May-03-2022, 01:39 PM
Last Post: Anldra12

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.