Python Forum
This is weird!!! It should work but I get an but I get a "ValueError: I/O operation"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
This is weird!!! It should work but I get an but I get a "ValueError: I/O operation"
#1
This is a simple assignment from one of my courses that I am doing. The assignment is to analyze a list of integer numbers and return True if the array contains a 3 next to a 3 somewhere in the list, and return False if it doesn't.

As usual, there are a billion ways to write the code but this is what I came up with:
def has_33(nums):
    length=len(nums)-1
    counter=0
    for i in nums:
        if i==3 and i==nums[counter+1]:
            print ("True")
            quit()
        else:
            counter = counter+1
            if counter==length:
                print("False")
                quit()

has_33([1, 3, 1, 3])
has_33([3, 1, 3])
has_33([1, 3, 3])
Okay, so the bottom three lines are the examples that were given to me to execute the code so it works. The topic of this course is "functions" so you can see that the exercise is designed to practice creating functions.

So here's what happens when I run the code:
1) When I run it in Jypiter notebook The third one, that should say true, works, but displays two "True" printouts. Not
sure why it does that. However, when I run it against the ones that are false (first two), I get a "list index out of range" despite the fact that I have written the code to avoid this error with a counter variable I set to 0 at first and if the counter == length-1 it should stop.

2) In Thonny, it runs the first example, gives me an output but won't run the other two. It should execute in sequence but doesn't. So, I stepped through the code using the debugger and when it reaches the quit(), I throws out a "ValueError: I/O operation on closed file." error. I did a search for this error on the internet and it says that the error is caused by trying to write a file that is already closed. I am not doing in file operations at all in this code so it isn't that, the other is an error due to improper indentation, but you can see my indentation is find as far as I can tell. Perhaps you guys can see where I screwed up with my indentations???

3) I tried running it raw on the command line and the same results for each one, it executed only one example and that was. I should be seeing two "False"s and one "True".

Anyways, I hope you guys can see what I am not seeing here, so I look forward to your posts.

Thank you!
Larz60+ write Mar-24-2021, 03:08 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags in future posts.
Reply
#2
I don't see why you would want to quit () after printing True or False only once. Try it like this as see if it does what you're expecting.

def has_33(nums):
	length = len (nums) - 1
	for i in range (length):
		if nums [i] == 3 and nums [i+1] == 3:
			return True
	return False

print (has_33([1, 3, 1, 3]))
print (has_33([3, 1, 3]))
print (has_33([1, 3, 3]))
Reply
#3
(Mar-24-2021, 02:25 AM)BashBedlam Wrote: def has_33(nums):    length = len (nums) - 1    for i in range (length):        if nums [i] == 3 and nums [i+1] == 3:            return True    return False print (has_33([1, 3, 1, 3]))print (has_33([3, 1, 3]))print (has_33([1, 3, 3]))
Ok! It's working now. Thank you! I replaced the "quit()" with "returns" as you suggested and it appears to work, both with the code I had written and what you had written.
Reply
#4
Happy to help Smile
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020