Home > database >  I'm new and I need some assistance with this Python script
I'm new and I need some assistance with this Python script

Time:06-15

I am currently reading the book Intro to Python for Computer Science and Data Science by Paul Deitel. I am on page 71 and it is asking me to run a script. Which is a part of the download files for the book and it does not run. I've gone to the file and opened it in note pad and copied the code and put it into a code editor (PyCharm) and on line 32 it says index out of range. So what do I do?

enter code here
# RollDieDynamic.py
"""Dynamically graphing frequencies of die 
rolls."""
from matplotlib import animation
import matplotlib.pyplot as plt
import random 
import seaborn as sns
import sys

def update(frame_number, rolls, faces, 
frequencies):
"""Configures bar plot contents for each animation 
frame."""
# roll die and update frequencies
for i in range(rolls):
    frequencies[random.randrange(1, 7) - 1]  = 1 

# reconfigure plot for updated die frequencies
plt.cla()  # clear old contents contents of current 
Figure
axes = sns.barplot(faces, frequencies, 
palette='bright')  # new bars
axes.set_title(f'Die Frequencies for 
{sum(frequencies):,} Rolls')
axes.set(xlabel='Die Value', ylabel='Frequency')  
axes.set_ylim(top=max(frequencies) * 1.10)  # scale 
y-axis by 10%

# display frequency & percentage above each patch 
(bar)
for bar, frequency in zip(axes.patches, 
frequencies):
    text_x = bar.get_x()   bar.get_width() / 2.0  
    text_y = bar.get_height() 
    text = f'{frequency:,}\n{frequency / 
sum(frequencies):.3%}'
    axes.text(text_x, text_y, text, ha='center', 
va='bottom')

# read command-line arguments for number of frames 
and rolls per frame
number_of_frames = int(sys.argv[1])  
rolls_per_frame = int(sys.argv[2])  

sns.set_style('whitegrid')  # white backround with 
gray grid lines
figure = plt.figure('Rolling a Six-Sided Die')  # 
Figure for animation
values = list(range(1, 7))  # die faces for display 
on x-axis
frequencies = [0] * 6  # six-element list of die 
frequencies

# configure and start animation that calls function 
update
die_animation = animation.FuncAnimation(
figure, update, repeat=False, 
frames=number_of_frames, interval=33,
fargs=(rolls_per_frame, values, frequencies))

plt.show()  # display window

CodePudding user response:

Below is the GitHub link to the author's source code for RollDieDynamic.py from Python for Computer Science and Data Science by Paul Deitel. This will help resole any indenting issues with your posted code.

https://github.com/pdeitel/IntroToPython/blob/master/examples/ch01/RollDieDynamic.py

Here is the author's note from the readme file. https://github.com/pdeitel/IntroToPython

NOTE: If you're trying to run the RollDieDynamic.py script in Chapter 1 and the window does not appear, this is due to a known issue in the software for which a fix is already in process. You can instead run the program with

ipython -i RollDieDynamic.py 6000 1

Then terminate IPython interactive mode (Ctrl D twice) after you close the script's window.

I am not familiar with ipython but here is a link to learn more about it. https://ipython.org/install.html

CodePudding user response:

This code is also available at github. Here is the link. https://github.com/pdeitel/IntroToPython/blob/master/examples/ch01/RollDieDynamic.py

The code given in github works absolutely fine. Here is a short video I made to prove there is no problem with the code. https://drive.google.com/file/d/1hrzOAWRS34U1vgfOehhWzuqRktndRJhC/view?usp=sharing

However, the error you are getting is probably due to indentations in your code. Make sure you are not using tab to indent and only space bars. Also, since the code you have posted has lost its indentation, probably some of your comments may be running in the code.

Solution Copy the code from the github link directly and run on IDE. Make sure to use the correct command i.e

python rolldiedynamic.py 6000 1

as given in the book.

Another way is to copy the code on either Google Colab or Jupyter notebook and run each command in a separate cell to find exactly why you are getting the error. Each line of code can be run individually in a cell. That way you will have a better understanding of what the code does and also lets you know where and why the error is being generated.

CodePudding user response:

I fixed it the erros on line 32 and 33. I changed the int(sys.argv[1]) and [2] to len(sys.argv[0]) and [0]

  • Related