Question: Q 4 Find the bug! 1 0 Points Grading comment: The client is furious There s a bug in their code! The client is furious

Q4 Find the bug!
10 Points
Grading comment:
The
client
is
furious
Theres
a
bug
in
their
code!
The client is furious Theres a bug in their code!
The program below is supposed to generate random samples for an experiment.
The program below is supposed to generate random samples for an experiment.
However, the client keeps trying to run the program and gets the error message,
However, the client keeps trying to run the program and gets the error message,
Error
writing
samples
to
file.
Error writing samples to file.
Find the bug or bugs in this code and correct it.
Find the bug or bugs in this code and correct it.
Upload a script containing your corrected program, and indicate in comments
Upload a script containing your corrected program, and indicate in comments
what you modified.
what you modified.
Given:
Given:
import random
SAMPLES_FILE = 'samples.txt'
def main():
""" Call generate_random_samples with the range of samples to generate
(low, high), as well as the quantity of samples to produce """
generate_random_samples(0,60,200)
def generate_random_samples(low, high, samples):
""" Generate the quantity of random integer samples in the range
specified (low, high) and write to text file """
try:
out_file = open(SAMPLES_FILE, 'w')
for _ in range(samples):
val = random.randint(low, high)
out_file.write(val +'
')
out_file.close()
except IOError:
print('Error opening the file.')
except ValueError:
print('Invalid data provided.')
except TypeError:
print('Error writing samples to file.')
else:
print('Success: Data written to file.')
if __name__=='__main__':
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!